Gnome 3 Background/Wallpaper Changer

There are a few programs in Linux that will change your user background for you randomly – and they are all well and good, but most of them lack the ability to customize maybe the hours certain folders will be included.  With the wave of people that are beginning to embrace Linux that are not all that tech-savvy, this post may be useful to you and help you discover ‘hidden’ abilities within Linux – or just a copy-paste solution. For those that are tech-savvy, this may help you find errors you may have encountered in your own scripts since you moved to Gnome 3, as there were a few changes, or it may just help you enable your script to work with Cron.

This entry is both a tutorial, as well as a complete solution for wallpaper/background changing within Gnome 3.

First, let’s start with directory structure – as it is the basis of what images will be chosen. Under your normal user account, create a directory where you want to store the images used for the wallpaper.  Be this  ~/.wallpaper or a non-hidden folder at ~/Pictures/WP or something similar.  Really, it doesn’t matter where you put it.  Inside that directory, you’ll create the directories that will be used for differentiation.  For example, you might have pictures of you and your friends in one folder, scenic pictures in another, and maybe ‘dark’ pictures in a third or something. So create the directories that will be used to separate the different kinds of pictures that you have – in this tutorial we will use two, one called day the other called night.

Now, for the script that will go in the main directory that you set up:

Now, to explain the script, line 1 is setting what type of script this is, which is a bash script. Line 2 saves the old IFS (Internal Field Separator) so that we can reset it after our use of it., line 3-4 sets the new IFS as a newline character.  Line 5 gets the directory name of the script – removing the need to hard-code the location where the wallpapers are. Line 6 gets the script’s filename, so that if the command was for help, we can properly show how to use the script.  Lines 7-8 will change the directory to the script’s directory and save it in a variable for our use later.

Lines 9-26 are purely to catch -help parameter and display how to use this script.  It can be removed with no ill side-effects other than removing the -help option.

Now, the fun part, line 27 detects if there was a parameter, with line 28 doing the lifting of finding all *.jpg and *.png files within the directory inputted. If there was no parameter, it will find all the valid image files within the script’s directory and all sub-directories.   The result of either is set to the pics array, which is why we set IFS to be a newline, separating each file found.  On line 32, we reset the IFS to its previous value.

Bash doesn’t have a way to find a random value between a min and a max built-in, so lines 33-36 handles that. 34 sets num to be the size of the array, 35 chooses a random number between 0 and 32767.  To knock that random number in to the range we need, we take the modulus if it by the max in line 36.  We use that number on line 37 to select one picture from our array.

Now, with Gnome 3 using a cron to change the background, we need to find the DBUS_SESSION_BUS_ADDRESS and set it so that gsettings has a clue of what to do.  So, we export the variable from the current user’s home directory on line 41.

Line 43 is the command used in Gnome 3 to change the background picture and set it to what was found on line 37.  Our final step it to output the selected picture’s filename in case we want to keep track of it.

So, now that you have a script that will change the background for you, you want it to run automatically.  To do this, run the command  crontab -e .  If you find you can’t edit your crontab, read this for some help.

We are going to set it up so that the day folder is used every 10 minutes from 9am until 4:59pm, (following the typical ‘9-5’ work day.) and we will use the night folder from 5pm until 8:59am. To do this, we need the full location of the script, so if you put it in ~/.wallpaper (as we will assume you did) your crontab will look like the following:

Replace <username> with your username.  And, that’s it!  You now have a Gnome 3 wallpaper changer with just a few lines of script and a properly set up crontab, no need to have an application constantly running to change it for you taking up extra memory.

No Comments Yet


There are no comments yet. You could be the first!

Leave a Comment

    Search the Blog