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
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
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.

for i in `find $DIR -iname *.jpg -o -iname *.png -o -iname *.gif`
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.


Anonymous said...

good one! i just added many some other new emo backgrounds for my blog

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.

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

STYLE=scaled # or tiled, zoom, etc
DIR="your wallpaper directory here"
for i in `find $DIR -iname *.jpg -o -iname *.png -o -iname *.gif`
let counter=counter+1;
gconftool-2 -t str --set /desktop/gnome/background/picture_filename ${pictures[$index]};
gconftool-2 -t str --set /desktop/gnome/background/picture_options $STYLE;