How To Shuffle Your Desktop Background Every So Many Minutes in GNOME and MATE

The GNOME and MATE desktops don't have a way to shuffle your desktop background images (wallpapers) automatically, but it's possible to make it happen automatically with shell scripts and a cron job.

Start with saving the pictures you want to use for your wallpaper images in a single directory.  All the ones I want to use are in ~/Pictures/Wallpapers/, so that's what I'll use in these scripts.

The biggest roadblock I ran into was figuring out why everything worked perfectly from a shell but failed without error from cron.  The reason is that gconftool-2 and mateconftool2 don't do anything unless the environment variable DBUS_SESSION_BUS_ADDRESS is set by gconfd and mateconfd.

The other confusing thing is that gconftool-2 doesn't modify anything in the MATE desktop, the default desktop of Linux Mint.  Because of this enlightening experience, the scripts presented are valid in both GNOME and MATE.  However, users of MATE must comment out the GNOME commands and uncomment the MATE commands.

Many of these steps can be done entirely from a shell.  To start one, go to Menu -> Accessories -> Terminal.

Step 1: Grab DBUS_SESSION_BUS_ADDRESS on login

In order to affect the desktop automatically, the DBus session bus address has to be extracted and saved as soon as you log in.

First, save a text file as ~/bin/login-script.sh with the following content:

#!/bin/sh
echo $DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus_session_bus_address

(You can name the file in the echo command whatever you want, but it's a dot-file in order to keep it hidden from ordinary listings.)

Run the echo command from a shell (or from the Run prompt accessed by Alt+F2) so that you don't have to log out for the shuffling to start.  Otherwise, just log out and log in again after you follow all the rest of these instructions.

Change the file permissions to executable.  To do that in a shell, use the command:

chmod a+x ~/bin/login-script.sh

Next, add it to the programs run every time you log in:

  • Menu -> Preferences -> Startup Applications
  • If you can't find it in the menu, then run the command:
    • In GNOME: gnome-session-properties
    • In MATE: mate-session-properties

On the "Startup Programs" tab, click on the Add button.

In the Add Startup Program window, fill in the fields as follows and click Save:

  • Name: Login Scripts
  • Command: /bin/sh -c /home/<your-user-name>/bin/login-script.sh
  • Comment: Script to run commands at login

Replace "<your-user-name>" with your actual user name.

Step 2: Save the shuffle wallpaper script

Create a new file as ~/bin/shuffle-wallpaper.sh and save it with the following content:

#!/bin/sh

# The file $HOME/.dbus_session_bus_address is made by
# $HOME/bin/login-script.sh every time the user logs in.
# It contains a dump of $DBUS_SESSION_BUS_ADDRESS so that the crontab
# entry and this script can affect the running dbus daemon.
#
DBUSFILE=$HOME/.dbus_session_bus_address
export DBUS_SESSION_BUS_ADDRESS=`cat $DBUSFILE`

# The location of the configuration tool.  It's gconftool-2 for GNOME
# and mateconftool-2 for MATE (the Linux Mint desktop).
#
GCONFTOOL="/usr/bin/gconftool-2"
#GCONFTOOL="/usr/bin/mateconftool-2"

# The location of the configuration key, depending on whether it's GNOME
# or MATE.
#
GCONFKEY="/desktop/gnome/background"
#GCONFKEY="/desktop/mate/background"

BGKEY="$GCONFKEY/picture_filename"
SHADEKEY="$GCONFKEY/color_shading_type"
SCALEKEY="$GCONFKEY/picture_options"
COLOR1KEY="$GCONFKEY/primary_color"
COLOR2KEY="$GCONFKEY/secondary_color"

# The directory with your wallpaper images.  Insert only the images you
# want to use for wallpapers here.  A single file is chosen at random.
#
WALLPAPERS="$HOME/Pictures/Wallpapers"
RANDOMIMAGE=`/usr/bin/find $WALLPAPERS -type f | /usr/bin/shuf -n 1`

# Choose a style and colors.  The default is zoom and solid black.
#
SCALED="zoom"; # Can be none, wallpaper, centered, scaled, stretched, spanned
SHADED="solid"; # Can be horizontal-gradient or vertical-gradient as well
COLOR1D="#000000000000"
COLOR2D="#000000000000"
if [ -f $HOME/.wallpaper-styles ]
  then
    SETTINGS=`grep "^$RANDOMIMAGE" $HOME/.wallpaper-styles`
    SCALE=`echo $SETTINGS | cut -s -d : -f 2`
    SHADE=`echo $SETTINGS | cut -s -d : -f 3`
    COLOR1=`echo $SETTINGS | cut -s -d : -f 4`
    COLOR2=`echo $SETTINGS | cut -s -d : -f 5`
  fi
if [ -z "$SHADE" ]; then SHADE="$SHADED"; fi
if [ -z "$SCALE" ]; then SCALE="$SCALED"; fi
if [ -z "$COLOR1" ]; then COLOR1="$COLOR1D"; fi
if [ -z "$COLOR2" ]; then COLOR2="$COLOR2D"; fi

# Change the wallpaper image based on the above settings
#
$GCONFTOOL --type string --set $BGKEY "$RANDOMIMAGE"
$GCONFTOOL --type string --set $SCALEKEY "$SCALE"
$GCONFTOOL --type string --set $SHADEKEY "$SHADE"
$GCONFTOOL --type string --set $COLOR1KEY "$COLOR1"
$GCONFTOOL --type string --set $COLOR2KEY "$COLOR2"

The script as shown is for the GNOME desktop.  If you use the MATE desktop, then comment out the GNOME-only lines and uncomment the MATE-only lines.  (A line is a comment if it starts with a hash sign, "#".)

Make the file an executable script.  To do this from a shell:

chmod a+x ~/bin/shuffle-wallpaper.sh

If you want to use any non-default options for a specific image, then save a new file as ~/.wallpaper-styles with the following content:

#
# Wallpaper styles
#
# Filename:Scale:Shade:Color1:Color2
#
# Filename should be the full path of the wallpaper image file
#
# Scale can be zoom, none, wallpaper, centered, stretched, or spanned
#
# Shade can be solid, horizontal-gradient, or vertical-gradient
#
# Color1 is the primary color or only color for solid
# The value is a 48-bit RGB value in the format #fcfcc4c48787
# #000000000000 is black, and #ffffffffffff is white
#
# Color2 is the secondary color for gradient shading
#
# Any wallpaper image not listed here will be shown with default settings
# See ~/bin/shuffle-wallpaper.sh for what the default settings are
#

Then add the wallpaper images you want to give special treatment to, one per line, with the options you want.  An example to show Galaxy.png centered with a solid black background color:

  • /home/<your-user-name>/Pictures/Wallpapers/Galaxy.png:centered:solid:#000000000000

You don't have to add any images except those you don't want treated with default settings.

Step 3: Add the cron job to do the automatic shuffling

The easiest way to add a cron job is from a shell:

  • crontab -e

At the bottom of the cron table, add one of the following lines:

Save and exit the crontab editor, and the new cron table will be installed.

That's it!  Your wallpaper image will be shuffled randomly, automatically according to the schedule you chose, starting with the top of the next minute it's scheduled to run.

Changes and undo

If you change your mind about the shuffle schedule, then re-run `crontab -e` (without the backticks) from a shell prompt and edit the shuffle-wallpaper line.

If you want to temporarily disable wallpaper shuffling, then re-run `crontab -e` from a shell prompt and place a hash sign ("#") at the start of the shuffle-wallpaper line.  This transforms the job into a comment.  Delete the hash sign when you want to re-enable the job.

If you want to permanently disable wallpaper shuffling, then re-run `crontab -e` from a shell prompt, and either place a hash sign at the start of the shuffle-wallpaper line or delete the line completely.

All Cards on This Page

 

Journals

2110 medium
Full list of journal entries