Gnome background changer

Gnome 2.3 on Ubuntu 10.04 LTS (Lucid Lynx) doesn’t appear to have an automatic wallpaper changer built in. While it appears there are many ways to accomplish this, I decided to write my own little script to get some time in with bash… alternative ways I have found include using “Wallpaper-tray” (which doesn’t appear to be in 10.04), Desktop Drapes, or a variation of the script below using things like “gconftool-2“.

To use this script, do the following:

  • Right-click on the desktop and choose “Change Desktop Background
  • Look through the backgrounds, and make sure the ones listed are the ones you want to have the script randomly select
  • Open up a text file using “gedit“, and add the contents of the script:
    #!/bin/bash
    installedDir=~/Pictures/backgrounds/random
    i=0
    while read line; do
       data=`echo "${line}" | grep -i filename`
       if [ "$data" != "" ]; then
          filenameCutLength=$((${#data}-11))
          filename=$(echo $data | cut -b11-$filenameCutLength)
          if [ "$filename" != "(none)" ] || [ "$filename" != 
          "random.img" ]; then
             extension=$(echo ${filename,,} | 
             cut -b$(expr ${#filename} - 2)-${#filename})
             if [ "$extension" == "jpg" ] || [ "$extension" 
             == "png" ] || [ "$extension" == "svg" ]; then
                fileArray[$i]=$(echo $filename)
                i=$(expr $i + 1)
             fi
          fi
       fi
    done << EOF
       $(grep deleted="false -A7 ~/.gnome2/backgrounds.xml)
    EOF
    fileArrayLength=`echo $[ ( $RANDOM % ${#fileArray[@]} ) ]`
    cp "${fileArray[$fileArrayLength]}"
    "$installedDir/random.img"
  • Save the file as “gRandomBackground.sh” in a directory (this example uses: “/home/$USER/Pictures/backgrounds/random”, if you install it elsewhere, be sure to change the $installedDir variable in the script)
  • Open “Terminal” and enter the command: crontab -e If you haven’t used crontab before, stick with the default, “nano
  • Paste the following as one line into the editor: 0 */3 * * * bash ~/Pictures/backgrounds/random/gRandomBackground.sh This will change the background every three hours, on the hour. “Ctrl+O, and then Ctrl+W” should get the crontab saved and also get you exited out of nano. For more information on how to use cron, see Google.
  • Open up the “Appearance Preferences” dialog again, by right-clicking on the desktop and selecting “Change Desktop Background
  • Click the “Add…” button and browse to the directory that gRandomBackground.sh is installed to
  • Select the “random.img” background, click the “Open” button, and then the “Close” button

Your background should change every three hours.