#!/bin/bash
#
# Description:
# This is a script to randomly select an image from a given directory
# and automagicly change the desktop background to the selected image
# in an Ubuntu Gnome environment. Developed and tested in Ubuntu 9.10
#
# License:
# You may freely use, modify and redistribute this script as long as you allow others to do the same.
# This script is provided without warranty or even fitness for purpose. Use at own risk.
#
############### Configuration #####################################
#
# Set this variable to wherever you store your background images
# Ubuntu default background storage is in /usr/share/backgrounds
BACKGROUND_LOC="/usr/share/backgrounds/"
#
###################################################################
#
### Code starts here. Do not modify below this line ###
## Pick new image function
pick_new_bg () {
backgrounds=( $( < ~/.background_temp) )
num_backgrounds=${#backgrounds[*]}
NEW_BACKGROUND=$(echo -n "${backgrounds[$((RANDOM%num_backgrounds))]}")
}
## Get and export the DBUS_SESSION_BUS_ADDRESS for crontab compatibility
nautilus_pid=$(pgrep -u $LOGNAME -n nautilus)
eval $(tr '\0' '\n' < /proc/$nautilus_pid/environ | grep '^DBUS_SESSION_BUS_ADDRESS=')
export DBUS_SESSION_BUS_ADDRESS
## Find image titles in specified directory
ls $BACKGROUND_LOC | egrep -i ".jpg|.png" > ~/.background_temp
## Pick new background image
pick_new_bg
## Get file name of current background and ensure the new background is different
CURRENT_BACKGROUND=$(/usr/bin/gconftool-2 --get /desktop/gnome/background/picture_filename | xargs basename)
while [ $NEW_BACKGROUND == $CURRENT_BACKGROUND ]
do
pick_new_bg
done
## Set the new background and remove the temp file
/usr/bin/gconftool-2 --type string --set /desktop/gnome/background/picture_filename $BACKGROUND_LOC/$NEW_BACKGROUND
rm ~/.background_temp
# The End.