Script day – randomly rotate GNOME desktop backgrounds

I kind of collect desktop wallpapers – I have a lot of those, several thousands1. It is a bit ridiculous as I mostly use maximized windows all the time so if not for the fact that in work I live on the console and I have a transparent terminal, I would rarely see my desktop wallpaper.

That being said, with a wallpaper collection, you want a software to manage it and cycle your desktop through the wallpapers. KDE has this function built it – just go to configure your wallpaper and select a directory of wallpapers, choose whether you want to cycle through the images sequentially or randomly, the delay and your done.

Not so in GNOME – simplicity for simplicity’s sake: I would have said that you get the same configurability as MS-Windows, but its not true – in GNOME’s “Background” configuration dialog, if you don’t use an image2 you can configure the background color to be a single color, a vertical or a horizontal color gradient (for the last two options you actually get to choose starting and ending color). MS-Windows users – eat your heart out! :-p

So what can a GNOME user do? There are a few 3rd party software that can tackle the task, at least one is even available in the repositories of most Linux distributions. Initially I tried wallpapoz which has a UI which is not really simplistic, and not really usable. Its written in Python and uses a weird client/server setup where the application UI is just a client that configures the server, and you have to run the server to actually get the functionality. Its not really comfortable to use under any circumstances and I also feel that its random selection could use some help (no reason for me to see the same wallpaper 3 times on the same day when you have more the a thousand images to choose from).

After I gave up on wallpapoz, I tried Desktop Drapes. Its has a much more modern UI and is written in GNOME’s “next-gen” programming framework: .net3, and it even promises to monitor the wallpaper directory for me and automatically add new wallpapers that appear there. Unfortunately it can’t really take the load of so many wallpapers on its UI, and while it runs fine as long as I don’t open the configuration window, it doesn’t looks like it holds up on its promise to add new wallpapers without resorting to using the nice looking but very unresponsive user interface.

When Desktop Drapes stopped working for me recently, I decided to go all scripty like – GNOME is using GConf as its internal configuration system, and GConf is a MS-Windows registry like configuration directory with a few obvious advantages:

  • It is rather small and simple in its layout and amount of stuff
  • It is very user-centric – while there is a system GConf registry, each user loads its own registry and no registry database other then the currently logged in user is actually loaded in a session
  • The backend storage for GConf is just a bunch of XML files, which is makes it easy to manage in case you need to (and you never need to)
  • It has both a graphical editor which shows help for each configuration value and a command line tool which you can use to manipulate the registry
  • And best of all – when you change a configuration value, it immediately takes effect!

A quick search in gconf-editor (the graphical GConf interface) brings up the following key:

/desktop/gnome/background/picture_filename

and changing it using

gconftool-2 -s /desktop/gnome/background/picture_filename string <path -to-image>

immediately applies the new desktop background.

To get our wallpapers to rotate automatically, all one needs to do is figure out a way to call gconftool-2 from cron, passing it a random wallpaper every time.

Bash to the rescue! Bash has a useful random number generator – just use $RANDOM for a 15 bit random number. For my purposes I think that 15 bit is a bit low (considering the number of wallpapers I have), so I use $RANDOM * $RANDOM for a bit more entropy. A quick hack and we end up with something like this:


WALLPAPERS_DIR=$HOME/Documents/Images/wallpapers
gconftool-2 -s /desktop/gnome/background/picture_filename -t string "$WALLPAPERS_DIR/$(ls $WALLPAPERS_DIR | head -n $(( $RANDOM * $RANDOM % $(cd $WALLPAPERS_DIR; ls | wc -l) + 1 )) | tail -1)"

Now to stick this into cron using crontab -e and you have “a poor man’s wallpaper manager” 🙂

A simple explanation of what is going on here – the heart of this one-liner is the $(( ... )) block which figures out a random number that corresponds to a file in the wallpapers directory – its basically RANDOM_VALUE modulo COUNT_WALLPAPERS + 1. the rest is just listing the wallpaper files and using head and tail to cut out just the file that I wanted4.

  1. mostly anime and video games fan made as well as promotional walls, a lot of hobby photographs – mine and other people’s, and a few more professionally made art []
  2. which is kind of hard to find how to do – apparently its the top left selection from the background images list, identified only by a tooltip that says “No Wallpaper” if you hover over it long enough []
  3. just bashing mono for fun, don’t mind me – I actually think mono is neat and .net is an interesting platform []
  4. for extra credit, try to use the unix command line tool “dog” as a head/tail replacement []

3 Responses to “Script day – randomly rotate GNOME desktop backgrounds”

  1. Elad:

    Is there any application you can recommend to the non-technical Windows captives? {*assuming, of couse, that they remain under windows 🙁 )

  2. Guss:

    eh. 🙂

    There are many – most of them cost thought. Eran can probably recommend you on the best one, but you might want to take a look at this list for starters: http://www.softplatz.com/software/windows-wallpaper/

  3. Eran:

    I use Wallpaper Changer. It’s kind of an oldie but it does everything it’s required of. It can even do transparent icon caption if you use old Windows. It even changes the startup wallpaper.
    On the laptop I use a different idea which is kind of easier. It’s a screensaver called gPhotoshow which you can feed your wallpapers into and it displays them any which way you want, even with music if you want, and when it’s done it can wallpaper the last image presented.

Leave a Reply