Padding Numbers in Filenames

I sometimes find myself with scads of files in a directory that, due to the vagaries of sort() do not show up in the “right” order. For example, 1.png, 2.png, … 751.png.

I was poking around with rename, but I wasn’t finding a good way to do this. Thankfully, a poster on TLUG showed me how to use sprintf to get the results I wanted, like so:

rename -n 's/(\d+)/sprintf("%04d", $1)/e' *.png

Very neat.

Go to Date WordPress Widget

This is the widget code to have a Go to Date widget in WordPress. It requires the correct permalink structure, but it is easy to paste into a text widget and it works nicely.

<script type="text/javascript">

function gotodate() {
  var baseurl = "http://example.com/index.php/"
  
  var y = document.getElementById("year").value;
  var m = document.getElementById("month").value;
  var d = document.getElementById("day").value;
  //debug alert(y + m + d);
  
  var newloc = baseurl + y + "/" + m + "/" + d
  window.location = newloc
}



<form>
  <table>
    <tr>
      <td>Year:</td>
      <td>Month:</td>
      <td>Day:</td>
      <td> </td>
    </tr>
    <tr>
      <td><input type="text" id="year" size="4" maxlength="4" /></td>
      <td><input type="text" id="month" size="2" maxlength="2" /></td>
      <td><input type="text" id="day" size="2" maxlength="2" /></td>
      <td><input type="button" value="Go" onclick="javascript:gotodate();" /></td>
    </tr>
</table>
</form>

Backdrop in Python

#!/usr/bin/python

import os, sys, random, imghdr

# This is a little program I call via cron to change my desktop every
# few minutes.  With no arguments it goes to my directory of backdrop
# images and picks a valid image at random.  If I specify a path and a
# file the program will put it up as the display.

# I don't want to fill up my inbox with emails from cron telling me that
# X isn't running, so I check first.
xisrunning = os.popen("pidof /usr/bin/X11/X").read()

def changebackdrop():
    # The below command works for transparent Eterm or Urxvt terminals,
    # populating their backgrounds with the image they occlude.  xli or
    # xsetroot can be called, but they don't work as desired for
    # transparent terminals.
    command = "/usr/bin/Esetroot"
    # If I was logging into X remotely, this would change.
    commandargs = " -display :0.0 "

    # This is where my backdrops live
    picdir = "/home/willyyam/misc/bmps/"

    if sys.argv[1:]:
        doit = command + commandargs + sys.argv[1]
        os.popen(doit, 'r')
    else:
        files = os.listdir(picdir)
        os.chdir(picdir)
        pics = []
        for file in files:
            # This is a test for valid images - it includes rgb files,
            # which are not supported by my image software, but the
            # error thrown is not terrible - it knows what it can and
            # cannot run.
            if imghdr.what(file):
                pics.append(file)

        randpic = random.choice(pics)
        doit = command + commandargs + picdir + randpic
        os.popen(doit, 'r')

if xisrunning:
    changebackdrop()
else:
    exit

# Copyright 2005 William Witteman