Random GNOME Wallpapers
If you’re anything like me you spend hours each day staring at your computer screen. We’re lucky to have nice graphical interfaces sprinkled with pretty pictures to make all the work we do seem more interesting, but after a time things can still seem dull.
One day I decided that it would be nice if the wallpaper on my GNOME desktop periodically changed to a random image. After looking for a solution somewhere which would allow this, and finding nothing, I decided that I should just go ahead and do it myself.
(No, you’re not experiencing deja vu. This is an old article from 2005 which I’ve decided to re-publish here. It was originally included in Linux Desktop Hacks.)
Selecting a Random Image
With a small Bash script, it is possible to randomly select an image from a directory and change the current GNOME wallpaper to that image. It’s easy to forget just how powerful Bash can be; more than just a simple command shell, Bash has a whole host of features which make it well suited for even quite complicated programming tasks. This task is actually quite simple compared to some of the things Bash can do.
To start this example, let’s assume you have a directory full of wallpapers somewhere, and let’s assume this directory is located at /home/adam/Images/Wallpapers/. The script we will write will take an image from that directory and set it as the current wallpaper. Here’s the first part of the script:
#!/bin/bash
export DIR='/home/adam/Images/Wallpapers/'
export NUMBER=$RANDOM
export TOTAL=0
The first line is a standard piece of code which says which program should be used to run the script (in this case Bash). If you get an error reporting a “Bad Interpretor” or something similar, you should check the documentation for your distribution and make sure that the path to the binary is correct (/bin/bash should be correct on most distributions).
After this line we store the location of the directory containing our images in the $DIR variable for future use. Next we store a random number, which is generated by the built-in variable named $RANDOM. We also set $TOTAL to zero to begin with; this variable will store the total number of images in the directory.
After this is a loop which counts the number of images in the directory, using the output of ls. Note that since there is no checking of file types performed it is important that only images are stored in this directory. Here’s the next part of the script:
for f in `ls $DIR`
do
let "TOTAL += 1"
done
let "NUMBER %= TOTAL"
The line let "NUMBER %= TOTAL" is the part that actually selects which image will be used. This line divides the randomly generated number by the number of images in the directory and stores the remainder of this division in the $NUMBER variable. If you’re wondering how this works, the remainder must be between zero and one minus the number of images (that’s simple maths), and since in the next part we start counting from zero it is possible for any image to be selected with this method.
The final part of the script simply counts through each image to see if it is the one which was selected. When it finds the correct image, it modifies the gconf setting which stores the filename of the wallpaper using the gconftool command. Nautilus notices this change immediately and updates the wallpaper. So, here’s the final script:
#!/bin/bash
export DIR='/home/adam/Images/Wallpapers/'
export NUMBER=$RANDOM
export TOTAL=0
for f in `ls $DIR`
do
let "TOTAL += 1"
done
let "NUMBER %= TOTAL"
export CURRENT=0
for f in `ls $DIR`
do
if [ $CURRENT = $NUMBER ]
then
gconftool -t string -s /desktop/gnome/background/picture_filename $DIR/$f
break
fi
let "CURRENT += 1"
done
Save the script somewhere convenient; for this example we’ll assume it’s saved as /home/adam/setbg. Also make it executable by running chmod +x setbg in a terminal. Now when you run this script your wallpaper will be changed.
Automating the Task
Using a utility called cron you can run this script automatically at set times. Explaining how to use cron is outside the scope of this article, but there are many different interfaces to cron which you can use. Simply set up cron to run the setbg script at set times; I personally set it to run at the start of each hour.
Also, using GNOME’s session manager you can have the script run when you login. You could even add a launcher to your panel so you can change the wallpaper with one click. Just enter /home/adam/setbg as the program name.