Members
..:: Login

Features
..:: Articles
..:: Wallpapers
..:: Linux Tutorial
..:: Mobile
..:: Motorola A1200 Hacks
Code: Bash Script to Change Gnome Background Randomly (Ubuntu)
by djlosch
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. 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\``

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.

Post Last Updated: Jan 14, 2007 7:08 pm
Related Articles
..:: Code: splitall.sh (bash); a cue file mp3 auto-splitter - Sep 28, 2006 7:47 am
Social Bookmarking (?)

StumbleUpon

Comments
Arnoques wrote on Friday, 2 May '08 - 1:21:28 AM -0400 [reply]
Thanks for the script! Very useful.
Just one thing, though: I had to change the random line to all_bgs=`echo `expr $RANDOM % $cnt + 1`` because $RANDOM goes from 0 to $cnt-1, so sometimes (when $RANDOM = 0) no wallpaper was loaded.

Robert wrote on Monday, 5 May '08 - 9:40:14 AM -0400 [reply]
very nice! I would though delete $temp_bg_list before exit or chmod it to give write access to all before exit if you have multiple users on your machine likely to use this. Particularly if they tinker with bg_path

djlosch wrote on Monday, 5 May '08 - 7:02:35 PM -0400 [reply]
the bg list is generated anew every time the script is run, so this only has that conflict is people use a diff bg_path or multiple people are logged in at the same time via xdcmp sessions or something.

alvynx wrote on Sunday, 11 May '08 - 8:24:43 AM -0400 [reply]
Thanks for your job!
It saves me a lot of work! :)

slicehax wrote on Monday, 5 February '07 - 3:09:37 PM -0500 [reply]
this script is awesome thanks

FreeStoring wrote on Tuesday, 11 December '07 - 5:15:54 PM -0500 [reply]
hay!!
good project :)
senks :)

Steven Van Ingelgem wrote on Tuesday, 27 February '07 - 2:41:05 AM -0500 [reply]
Please find an updated script, which allows the backgrounds to contain spaces:
#!/bin/bash
# script: change_backgrounds.sh - bash version
bg_path=/home/steven/.wallpapers/
temp_bg_list=/tmp/bg_change_list
extensions="jpg png gif jpeg JPG GIF PNG"
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``
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
Greetz

Steven Van Ingelgem wrote on Wednesday, 25 April '07 - 4:37:29 PM -0400 [reply]
Hi there again :)
A better version (more suitable for me, and more random) is to use the /dev/urandom device like this:
all_bgs=`echo $((`head -c4 /dev/urandom| od -An -tu4`%$cnt))`
you could then also change the !/bin/bash to !/bin/sh without a problem :)
My url is: http://www.salvania.be
Greetz ;)

SantaWithoutClause wrote on Wednesday, 27 June '07 - 10:05:35 AM -0400 [reply]
Cheers guys, much appreciated, this should make things a bit more fun ;)

Add Comment
Name:

Comment:


Please do leave a comment as I love to get feedback from visitors.
  • All fields are required, but your real name is not required.
  • Plain URLs (once again, no HTML or BBcode) will change to clickable links after 72 hours.
  • Comments are of the opinion of their author, not myself, and are not endorsed by myself.
  • Spam will immediately call upon the wrath of the BanHammer.