Programmatically changing Ubuntu wallpaper

I've got a rotating background on my Ubuntu desktop at work, and you can have one too. Here's how, using GNOME or Unity (basically GNOME-driven, as far as I can tell.)

Firstly, for clarity and sanity's sake, you should save the following to a bash script file:

#!/bin/bash
 
# Cron won't be able to connect to your Gnome session without this
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`
 
# This changes the wallpaper setting: it updates immediately
gsettings set org.gnome.desktop.background picture-uri $1

This script does a couple of things that you wouldn't want to have to remember every time:

  1. it works out how to get a background command to communicate with your active session and then
  2. uses that information to set the wallpaper dynamically in your current session, where the image file it uses is the first argument to the bash script ($1) in "URI" format (so either http://... or file:///....)

Personally, I saved it to /opt/wallpaper.sh, but it can go anywhere. The main thing is that, once it's saved, run chmod +x /opt/wallpaper.sh - or wherever you saved it to - to make it executable as a command.

Next, I used Gimp to create several variations on the following image (if you know who owns its original copyright, tell me!)

Coffee cup as a laptop battery discharge meter

with successive battery "bars" filled to grey. I then edited Ubuntu's scheduled "cron" task by running crontab -e and adding the following:

# Coffee meter empties through the morning
20  9 * * * /opt/wallpaper.sh file:///.../coffee-04.png
10 10 * * * /opt/wallpaper.sh file:///.../coffee-03.png
0  11 * * * /opt/wallpaper.sh file:///.../coffee-02.png
50 11 * * * /opt/wallpaper.sh file:///.../coffee-01.png
40 12 * * * /opt/wallpaper.sh file:///.../coffee-00.png
 
# At around 1pm, I have my second cup of coffee!
20 13 * * * /opt/wallpaper.sh file:///.../coffee-04.png
10 14 * * * /opt/wallpaper.sh file:///.../coffee-03.png
0  15 * * * /opt/wallpaper.sh file:///.../coffee-02.png
50 15 * * * /opt/wallpaper.sh file:///.../coffee-01.png
40 16 * * * /opt/wallpaper.sh file:///.../coffee-00.png

Result? I have a coffee cup background image, which magically fills up just after 9am, then gradually empties until lunchtime; at which point it fills up again and empties by the end of the working day.

Now, if only I could get Ubuntu to actually make the coffee for me, I'd be sorted.

Comments

Hey, this is awesome, thanks a lot!
To your last paragraph: take a look at this - I'm certain, you can run a cronjob for it, too!
http://moccapi.blogspot.co.at/
Yours,
Lukas

Thanks for the feedback; and I like the idea of MoccaPi. If only I could get it to control an Aeropress....!