Gnome/Unity random wallpaper script

By Dmitry Kann 2 min read
This post  in Russian

Note: this script will not work in GNOME 3 and Ubuntu 11.10; for these systems you have to use the new version.

This bash script picks up a random image file (.jpg, .jpeg or .png) from the predefined directory and sets it as wallpaper.

One of the tricky things was to make it work when started from cron. The problem was with the DBUS_SESSION_BUS_ADDRESS variable (required by gconftool-2) not set in that case, so there’s an extra piece of code that takes care of this variable.

The code has been tested with all Ubuntu versions up to 11.04 Natty Narwhal. It works with both Gnome and Unity.

The only thing to setup in the below script is the path to image files in dir_wallpapers.

# Description
#   Script to set a random wallpaper in Gnome/Unity
# Synopsis
#   rotate-wallpaper
# Author
#   Dmitry Kann,
# License
#   Public domain
# Setup vars
dir_wallpapers="$HOME/Pictures/"  # Path to wallpaper directory

# Exports DBUS variable which is not set when called from cron session
export_variables() {
pid=$(pgrep -u $user gnome-panel)
for dbusenv in $pid; do
  eval "export $data"

# Fill array with picture files (replace spaces with colons to fool the array)
files=( $(find "$dir_wallpapers" -type f \( -iname '*.jpeg' -o -iname '*.jpg' -o -iname '*.png' \) | sed s/' '/':'/g) )
# Get number of members in the array
# Randomize N
# Get the normal file name (change colons back to spaces)
file=${files[$N]//:/ }
# Change desktop wallpaper
gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$file"
Subscribe to blog updates: