Shell script changing desktop wallpaper

xralf picture xralf · Apr 5, 2011 · Viewed 25.2k times · Source

Could you write the easiest possible shell script that will change the desktop wallpaper (in Ubuntu) in regular intervals (e.g. 1 minute).

Wallpapers will be saved in particular directory (e.g. $HOME/wallpapers). I need only basic functionality.

1) select random wallpaper from $HOME/wallpapers
2) set it as wallpaper on desktop
3) set cron to run the script every minute (not part of the question).

Answer

tamasgal picture tamasgal · Apr 5, 2011
#!/bin/bash
wallpaperdir='$HOME/wallpaper'

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$randompic"

Save this script and edit your with the command "crontab -e" (it launches an editor where you put this line at the end of the file):

*/1     *     *     *     *         /bin/bash /path/to/script.sh

edit: I assumed you're using gnome. If not you need to edit the last line, because my example uses the Gnome Conftool. ;)

To change the background in XFCE, you should change the line with gconftool-2 to:

echo -e “# xfce backdrop list\n$randompic”>$HOME/.config/xfce4/desktop/backdrops.list    
killall -USR1 xfdesktop