Display Random Image on Desktop

Here's a little script that randomly selects one file from a directory of image files and displays it as the desktop background. This works with Gnome 2.2 (and perhaps other versions of Gnome 2.x as well). It is a bit different from similar scripts found on Gnome Hacks in that it can handle blanks in file and directory names.

#!/path/to/bash

IMAGE_DIRECTORY=/path/to/image/directory
FORMAT='*.jpg'

NUM_OF_IMAGES=`ls $IMAGE_DIRECTORY/$FORMAT | wc -l`
if [ $((NUM_OF_IMAGES)) -gt 0 ]
then
   ((N=RANDOM%NUM_OF_IMAGES))
   IMAGES=`find $IMAGE_DIRECTORY -name $FORMAT -printf "%p\r"`
   IMAGE=`echo $IMAGES | awk -F"\r" '{ print $(n) }' n=$N`

   gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$IMAGE"
fi

For beautiful images suitable for desktop backgrounds I highly recommend Garrett LeSage's site, linuxart.


Sébastien Biot — Tue, 19 Oct 2004 19:28:45 +0000