Automatic Desktop Wallpaper Changer for Linux


I have a lot of wallpaper that I wanted to switch automatically. After a lot of search i found that there a command way to control all the gnome settings. So I the command I used is “gconftool-2“.
So code is like:

#!/bin/bash
folder="/home/rsrijith/Pics" #folder where the pics lie in
find $folder -iregex ".*.jp.?g" >$folder/temp_back #list all images in the folder
no=$(cat $folder/temp_back|wc -l)
rand=$(echo `expr $RANDOM % $no`)
while ( grep -q $rand $folder/.last50 )
do
rand=$(echo `expr $RANDOM % $no`)
done
echo $rand >> $folder/.last50
tail -50 $folder/.last50 >$folder/temp
cat $folder/temp >$folder/.last50
rm $folder/temp
file=$(sed -n $rand'p' $folder/temp_back)
ON_USER=rsrijith #your username here

export DBUS_SESSION=$(grep -v "^#" /home/$ON_USER/.dbus/session-bus/`cat /var/lib/dbus/machine-id`-0)

sudo -u $ON_USER $DBUS_SESSION /usr/bin/gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$file" && echo "Worked On:"$(date) >>/home/$ON_USER/logs/desktopbackground
echo "Set Wallpaper as:"$file >>/home/$ON_USER/logs/desktopbackground
rm $folder/temp_back

I have used the DBUS_SESSION variable to make it compatible with Ubuntu 8.10. The Dbus session is a bit bit different from earlier versions. The change is

that it does’nt accept the change until the gnome is restarted. It’s due to the new session that is made each time you update that value.

So the folder variable should the folder where the wallpapers should lie. You can put pictures in folders inside that folder. All of it will be taken. But I have specified to take only jpg and jpeg only.Now a random image is picked from that using the environment variable $RANDOM. Next we update the gnome settings using gconftool-2 if that image was’nt used in the last 50 changes.

Thus this file picks an image and puts it as wallpaper. So the next part is making this change automatically. For this we use cron (linux scheduler).
Use the command below to enter the crontab

sudo crontab -e

Enter the crontab entry as

*/15 * * * * /home/rsrijith/shell/deskback 2>>/home/rsrijith/logs/deskbackground

Check the entry using

sudo crontab -l

That’s all!!!
Anyone tried? Worked? Comment me !!!


, , , , , , , ,

  1. #1 by Patrick on October 26, 2010 - 2:25 pm

    no comments yet? ..unbelievable! Thanks for your script.. so simple that i dont need any additional software (that wont work with ubuntu maverick anymore). THANKS!:-)

  2. #2 by Benoit Segond von Banchet on January 19, 2011 - 8:52 am

    line 19: in my Bash (4.1) I must change the ‘&&’ simply by ‘&&’ (without the quotes)
    And then: IT WORKS!!!!
    Thank you for a nice script and a learning experience!

  3. #3 by Bénoît Segond von Banchet on January 25, 2011 - 12:06 am

    I played some time with your ideas and adapted it for my Xfce4.6 environment as follows:

    #!/bin/bash
    #User for whom the wallpapers are changed
    User=benoit
    #folder where the pics lie in
    Folder=’/home/share/localdomain/nature’
    ################################################################################
    #cd into home-dir
    cd /home/$User
    #make local list of images
    find $Folder -iregex “.*.jp.?g” > .wallpaperchanger/listavail
    #count them
    No=$(cat .wallpaperchanger/listavail | wc -l)

    #choose one at random till we have one that is not recently used
    Rand=$(echo `expr $RANDOM % $No`)
    while ( grep -q $Rand .wallpaperchanger/recent )
    do Rand=$(echo `expr $RANDOM % $No`)
    done
    #this one will be used:
    echo $Rand >> .wallpaperchanger/recent

    #keep track of 50 most recent used images by fifo
    tail -n50 .wallpaperchanger/recent > .wallpaperchanger/fifotmp
    cat .wallpaperchanger/fifotmp > .wallpaperchanger/recent
    rm .wallpaperchanger/fifotmp

    #Now setup a “list” for xfdesktop to “choose” from
    echo ‘# xfce backdrop list’ > .config/xfce4/desktop/backdrop.list
    echo $(sed -n $Rand’p’ .wallpaperchanger/listavail) >> .config/xfce4/desktop/backdrop.list

    #Find users current display:
    Display=$(xauth -f .Xauthority list | head -n1)
    #echo $Display
    Display=${Display%% *}
    #echo $Display
    Display=:${Display#*:}
    #echo $Display

    ##SHOW!!
    sudo -u $User env DISPLAY=$Display xfdesktop –reload

(will not be published)


  1. Yahoo Healthy News