Gnome/Unity random wallpaper script

By Dmitry Kann 2 min read This post in Russian 0106

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"

Do you like this post? Feel free to subscribe to updates of my blog.

Subscription options


More to Read

Post image
Ubuntu/GNOME 3 XML wallpaper creation
Post image
Ubuntu/GNOME 3 random wallpaper script
Post image
Sound Switcher Indicator ‣ Sound Switcher Indicator for Ubuntu
Post image
Watermarking pictures for Picasa Web Albums
Post image
Ymuse ‣ 0.13
Post image
Sound Switcher Indicator ‣ 2.3.4