Howto time-controlled change the wallpaper and theme in Ubuntu 14.04

In this article I describe how to set up a time-controlled, automatic change of the wallpaper and the theme of Unity (GTK 3). I like to have a dark theme and wallpaper at night:

My Ubuntu desktop at night.

My Ubuntu desktop at night.

By day I prefer a bright theme and wallpaper:

My Ubuntu desktop by day.

My Ubuntu desktop by day.

So my solution works at follows. I use the following script that performs the change:

#!/bin/sh

# export DBUS_SESSION_BUS_ADDRESS environment variable
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

hour=$(date +%H)
if [ $hour -ge 8 -a $hour -le 18 ] ; then
  echo "Setting theme and wallpaper to day"
  gsettings set org.gnome.desktop.background picture-uri "file:///home/civic/Bilder/Wallpapers/beautiful_winter_scene-wallpaper-1920x1080.jpg"
  gsettings set org.gnome.desktop.interface gtk-theme 'Gnome-Cupertino'
else
  echo "Setting theme and wallpaper to night"
  gsettings set org.gnome.desktop.background picture-uri "file:///home/civic/Bilder/Wallpapers/Firefox_wallpaper.png"
  gsettings set org.gnome.desktop.interface gtk-theme 'OMG'
fi

First it is necessary to determine the address of the dbus session to make gsettings working. Usually you use it in a gnome-shell where this environment variable exists. This is not the case if you use this script with cron. We then determine the current hour and distinguish between day and night by comparing it with 8 and 18. So it is day after 8 (am) and before 19 (7 pm). The path to the wallpapers must be changed to your desired pictures. I use the themes OMG and Gnome-Cupertino. You can find themes and instructions how to install them at http://gnome-look.org/index.php?xcontentmode=167. Save this script for example under “set theme” and use the following command to make it executable:

chmod +x settheme

Now we have to start the script when it is time for a change. A handy tool is gnome-schedule. You can install it with the command:

sudo apt-get install gnome-schedule

Here is a screenshot of gnome-schedule.

Screenshot of Gnome-Schedule, a tool to schedule tasks.

Screenshot of Gnome-Schedule, a tool to schedule tasks.

Click new for a new task. Enter a descriptive “Description“. “Command” is the path to our new script. Select that it is an X application. Since we want to start it once when the day starts, select “Advanced” and enter the time 8h and 5m, so it is called 5 minutes after the day starts. As you can see in the screenshot, a second entry is necessary to change theme and wallpaper to night. It is basically the same entry but with a different time.

Since we also want to right theme and wallpaper when our computer is not running at 8:05 and 19:05, we have to use another application called “Startup Applications”. Just add a new entry pointing to that script.