Sunday, January 27, 2008

How to change GNOME desktop wallpaper programmatically

GNOME preferences are managed by the GConf tool. Each preference in the GConf repository is expressed as a key-value pair.
gconftool-2 -R /desktop/gnome
command displays all the preferences listed under "/desktop/gnome" repository. You can print the value of a key in the gnome preferences table with the "g" parameter.

ilkinulas@tututil:~/bin$ gconftool-2 -g /desktop/gnome/screen/default/0/resolution
1280x1024
The "R" option prints all subdirectories and entries under a directory, recursively.

ilkinulas@tututil:~$ gconftool-2 -R /desktop/gnome/background
color_shading_type = solid
secondary_color = #dadab0b08282
primary_color = #000000000000
picture_filename = /home/ilkinulas/Pictures/wallpaper/w1209.jpg
picture_options = scaled
picture_opacity = 100
draw_background = true
ilkinulas@tututil:~$ gconftool-2 -g /desktop/gnome/background/picture_filename
/home/ilkinulas/Pictures/wallpaper/w1209.jpg
You can set a key to a value with the option "s". When you set a gnome preference, applications that are interested in that preference will be notified. For example:

gconftool-2 -t str -s /desktop/gnome/background/picture_filename "/home/ilkinulas/Pictures/wallpaper/64269-1.png"
sets gnome desktop background picture to "/home/ilkinulas/Pictures/wallpaper/64269-1.png". The option "-t" is used to specify the type of the value of the key. If you execute the command above you will see that your desktop wallpaper is changed. Now lets write a shell script to randomly select a picture and set selected picture as wallpaper.

#!/bin/bash
DIR=/home/ilkinulas/Pictures/wallpaper
counter=0;
for i in `find $DIR -iname *.jpg -o -iname *.png -o -iname *.gif`
do
pictures[$counter]=$i;
counter=$counter+1;
done
index=$((RANDOM%${#pictures[*]}));
gconftool-2 -t str --set /desktop/gnome/background/picture_filename ${pictures[$index]};

If you do not want to execute the above script manually you can add a new crontab entry. I start working in the office at 8 am and I use "00 09 * * *" as my crontab schedule, I want my wallpaper change everyday at 9 am, if my computer is on ;)

If the way I explain in this post is too complicated you can try Webilder. Webilder delivers stunning wallpapers to your Linux desktop, directly from Flickr and Webshots. You choose what keywords (tags) to watch for, and photos are automatically downloaded to your computer. Webilder can also change the wallpaper every few minutes.

2 comments:

Anonymous said...

good one! i just added many some other new emo backgrounds for my blog
http://www.emo-backgrounds.info

Don PePE`s said...

hi, i have been trying to run your script but had some fails. Here is tweaked to work with names that contains spaces.
salu2

i added "let" to the counter variable, because it don't work in the original way.



#!/bin/bash
STYLE=scaled # or tiled, zoom, etc
DIR="your wallpaper directory here"
counter=0;
IFS=$'\n'
for i in `find $DIR -iname *.jpg -o -iname *.png -o -iname *.gif`
do
pictures[$counter]="$i";
let counter=counter+1;
done
index=$((RANDOM%${#pictures[*]}));
gconftool-2 -t str --set /desktop/gnome/background/picture_filename ${pictures[$index]};
gconftool-2 -t str --set /desktop/gnome/background/picture_options $STYLE;