Random Wallpapers in Gnome

There are many softwares in Windows(TM) which changes the wallpaper randomly from a given set of images. I was thinking of this for my Linux machine. I came up with a shell script. Though there can be many other ways. I just wanted to share my script and nothing else.

Update: There’s a tool which does all this automatically. drapes in Universe repository

Here in this code I am assuming that you have got all your wallpaper images in the directory /home/username/Pictures . You have to replace this directory with your wallpaper directory.

Here goes the code :

#! /bin/bash
wallpaper_folder=/home/username/Pictures
wallpaper_array=($wallpaper_folder/*.jpg)
number=${#wallpaper_array[*]}
((number=RANDOM%number))
random_wallpaper=${wallpaper_array[$number]}
gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$random_wallpaper"

Well, so let us quickly analyze the code. First line is the famous ‘sha-bang’.

In the second line I am just setting the path of the wallpaper folder to the variable wallpaper_folder.

Third line I am creating an array containing all the file names with the extension jpg in the directory ‘wallpaper_folder’ (spaces are being escaped). More about bash arrays here.

Fourth line, I am calculating the number of elements in the array. This is the standard method of calculating the number of elements in a bash array. (replacing # with @ will also work fine).

Then in the fifth line, random number is generated within the range 0 to ‘number’. More about random numbers in bash here.

Sixth line set the variable random_wallpaper to the value corresponding to the key ‘number’ in the array wallpaper_array.

Seventh line, finally sets the file ‘random_wallpaper’ as the wallpaper. gconftool-2 is used to do customization in GNOME settings. Check out the man pages of gconftool-2 to know more about it. There’s a GUI tool for this gconftool-2 : gconf-editor

Now this script just sets the wallpaper randomly from a given set of images. Now you can schedule this script using cron to execute it at some regular interval. (In case you dont know what is cron, check out the manual pages of cron and crontab).

This entry was posted in GNOME, Linux and tagged , , . Bookmark the permalink.

3 Responses to Random Wallpapers in Gnome

  1. agatzebluz says:

    I use Drapes, which is in in the Universe repositories and it works fine.

  2. spsneo says:

    @ agatzebluz

    I didnt know about this tool.
    I tested it just now. and its a nice one.

    Thanks

  3. Dak says:

    I used wallpaper-tray which is also good, and allows you to select graphic files from multiple directories. It also has a feature called wallpaper which allows the user to search the workstation for files that can be used as wallpapers.

    I tried drapes a few Ubuntu versions ago, but it kept crashing at the time.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>