Sunday, November 16, 2008

Automatic Desktop Wallpaper Changer

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

No comments:

Post a Comment