MTB3: Yet another (very simple) background switcher for Gnome 3

Way back when I started playing around with Python, one of the first applications I wrote was a transitioning background application. It worked quite nicely, but it was very tied to the Gnome 2 way of doing things and not particularly suited to a conversion to Gnome 3.

So now I have a simplified version which (after a bit of frustration) works quite nicely by simply displaying a random picture from my ~/Pictures/Backgrounds folder. The script, as it stands, looks like this:

#! /usr/bin/python
""" Yet another, veryt simple background switcher for the Gnome 3 Desktop
 Copyright (C) Paul Pritchard 2013 
 MTB3 is free software: you can redistribute it and/or modify it under the 
 terms of the GNU General Public License as published by the Free Software 
 Foundation, either version 3 of the License, or (at your option) any 
 later version.
 MTB3 is distributed in the hope that it will be useful, but WITHOUT ANY 
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 You should have received a copy of the GNU General Public License along
 with this program.  If not, see ."""

import os
import random
import mimetypes

backgrounds = os.environ['HOME'] + "/Pictures/Backgrounds/"
pictures = []

for filename in os.listdir(backgrounds):
    mimetype = mimetypes.guess_type(filename)[0]
    if mimetype and mimetype.split('/')[0] == "image":
        pictures.append (filename)

picture = random.randrange (0, len(pictures))
fullpath = '"file:///' + backgrounds + pictures[picture] + '"'
os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '%s'" % (fullpath))

I then put a short script in /usr/local/bin so that I could execute it with the mtb command and, since everything was working swimmingly, I turned to cron to launch the app every 20 minutes.

The crontab line looks like this:

*/20 * * * * mtb

And it works, even if I have spent far too long finding my way around gsettings.

There are a couple of points worth noting, if only for my own future sanity.

Firstly, gsettings needs to know what screen to run on. This is fine when the command is executed from a terminal because it knows to run on the screen the terminal is sitting in. But when launching the same command from crontab, there is no terminal. You need, therefore, to include the DISPLAY=:0 GSETTINGS_BACKEND=dconf to tell gsettings where to run.

Secondly, the crontab environment settings are not always as complete as those found in Bash. So while I am able to enter just the command in Frugalware, other distributions may require you to use the full path to the command.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s