Local

..:: Home
..:: Legal
..:: Contact
..:: About
..:: RSS
..:: Log in

Links

..:: Fake Bill Gates
..:: Tap the Hive

A Random Quote

"A foul's a foul, and a bat's a bat, even if it's flat. 10 feet on the Westside, is 10 feet on the Eastside. Football is football .... unless it's futbol. Now, a win's a win, and a loss is a loss. But no matter what, you'd better come with it, because it's 90 feet to first, no matter where home is."

Bash Script to Randomly Change Gnome Background

This code uses a backgrounds folder, set as $bg_path at the beginning of the script. Just drop it in your cron.hourly folder and let it run. It will change your background on all desktops every hour (or whatever you set in cron). The script supports PNG, JPG, and GIF images, and you can add more by just changing the extensions variable at the top.

The Script

#!/bin/bash
# script: change_backgrounds.sh - bash version
# version 2007.3.7
# description: randomly replace gnome background with one from a directory
# credits: David Loschiavo [http://www.djlosch.com], Steven Van Ingelgem
# license: GPL

bg_path=/mnt/archive/media/pics/backgrounds
extensions=”jpg png gif jpeg JPG GIF PNG”
temp_bg_list=/tmp/bg_change_list

rm -f $temp_bg_list

for extension in $extensions
do
	find $bg_path -iregex “.*.$extension” >> “$temp_bg_list”
done

cnt=`wc -l “$temp_bg_list” | cut -f1 -d” “`
all_bgs=`echo `expr $RANDOM % $cnt + 1“

selected_bg=`head -n$all_bgs “$temp_bg_list” | tail -n1`

logger “Changed desktop to: $selected_bg”

gconftool-2 -t string -s /desktop/gnome/background/picture_filename “$selected_bg”
exit 0

The previous version (2007.1.14) did not account for spaces in filenames. Steven Van Ingelgem made the necessary changes, and I have put them up here. Thanks Steven!

..:: Installation

There are multiple ways to set this to run. The first is to just drop it in your cron.hourly folder and set the permissions.

sudo wget http://www.djlosch.com/scripts/change-background.sh -O /etc/cron.hourly/change-backgrounds.sh

Then change the permissions to allow it to run.

sudo chmod 700 /etc/cron.hourly/change-background.sh

Now edit the file and set bg_path to wherever your backgrounds are stored. DO NOT leave any spaces outside the double quotes.

Acceptable: bg_path=”/home/your_username/backgrounds”
Not Acceptable: bg_path = “/home/your_username/backgrounds”

Finally, to make sure the backgrounds don’t appear funny, right click on the desktop and go to “Change Background”. Then make these setting changes:

Style: Scaled
Desktop Colors: Solid Color
{Color Palette->Color Name}: #333333

The rest of the settings you can leave unchanged. You can customize the <Color Name> to something other than a gun metal color, but that’s what I prefer.

If you want the background to change at a different interval, check one of the many crontab tutorials out on the web.

Leave a Reply