A self-scheduling MTB

Just a quick note to mention that I have started thinking about making MTB a bit more configurable (in terms of folders and wait times, so far). The upshot of this is that it no longer uses cron for the scheduling. The application now needs to be added to your Gnome start-up applications so that it can schedule itself depending on the (so far, single) option available.

More will follow but, as ever, you can find the latest working version of the source over on GitHub.

MTB: Now with flexible folders

About a week ago, I mentioned that there were some more changes for MTB on the way and the main one of these is now done. Instead of being locked into fixed folders and fixed changeover times, this background switcher now supports a more flexible folder structure.

You still need to place the backgrounds folders in ~/Pictures/Backgrounds but, instead of being locked into only three (named) named folders with fixed cutover times, you can now use up to 24 folders.

Each of the folders needs to be named xx where xx is a two digit number reflecting the hour in which that folder will start to be used. The script will then continue to use that folder until the hour in which the next folder should be used.

If, for example, you have two folders named 08 and 17, MTB will start using the images in folder 08 at 08:00 and then switch to using folder 17 at 17:00.

As ever, the source can be found on GitHub and you are free to view, use, modify and share it (under the GPLv3 licence).

MTB: Quick Update

I mentioned, earlier this week, that I have finally gotten around to rewriting my simple background switcher so that it actually works under Gnome 3. At the time I was intending to tidy up the code a bit first and then push it all to the master branch.

Well, I haven’t gotten around to making the changes I intended (yet), but have pushed it to the Master anyway. The current development branch for MTB is 0.3 and, as soon as I have some time, I will see about making it all work a bit more generically.

MTB: A simple background switcher for the Gnome Desktop environment

Two years ago this month, I knocked together a simple background XML powered background switcher for Gnome 2.

Time marches on, Gnome 2 was replaced by Gnome 3, and I kept on intending to update the background switcher so that I could keep on using it. But instead, I’ve rewritten it.

I actually posted the basic script on here last month, but have since extended the functionality somewhat. You can find the latest version of MTB on GitHub (on branch 0.3, for now) and, once I have tidied it up a bit I will push this to the master branch. Because I’m a natural hoarder, I have hung on to the code for the Gnome 2 version of this – which can be found on branch 0.2, obviously enough.

I have also updated the MTB Project Page on this site. Since the instructions here were simply copied and pasted from the README file, I have removed the online duplicates to avoid confusion.

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

 mtb.py
 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.

Better help text with argparse

Way back in 2011, I built a small application to build a Transitioning Background XML which could then be fed into Gnome 2 so that I could distract myself with a constantly changing background. This worked well enough and achieved what I wanted – an easy way to rebuild the XML every time I wanted to add some more photos. It didn’t provide any help text, though, which is something I kept meaning to sort out.

And then Gnome 3 came along and everything changed.

I have now gotten around to looking at Macsen’s Transitioning Background again and have finally found the time to replace getopt with argparse. What that means in English is that if you type mtb -h, you will see this:

usage: mtb.py [-h] [-p PATH] [-s STATIC] [-t TRANSITION]

Create a transitioning background XML for Gnome 2

optional arguments:
-h, --help show this help message and exit
-p PATH, --path PATH Set path for images and XML
-s STATIC, --static STATIC
Set static time in seconds
-t TRANSITION, --transition TRANSITION
Set transiton time in seconds

As ever, all of the source can be found on GitHub.

Cleaning up some code

It was in the middle of last year that I implemented what I rather ambitiously referred to as a wallpaper switcher for Gnome 2. What Macsen’s Transitioning Background does is generate an XML file based on the images in a selected folder and then let the Gnome background switcher handle the rest.

With the switch to Gnome 3 this functionality is no longer supported. At least, not as far as I can tell.

I do still like the idea of using an XML file to control the background and have been toying with the idea of knocking together myself. As a first step towards this, I have tidied up some of the code.

The latest version of the source can be found on GitHub. The generated XML can be used to power a transitioning background in Gnome 2 and – if I ever find the time – I will start knocking together something that will work under Gnome 3.

Macsen’s Transitioning Background: An XML powered wallpaper switcher for Gnome 2

While on holiday last month, my four-year-old son got hold of my camera and started taking photos. Lots of photos, some of which were quite good. Being the proud father that I am, I decided I wanted my desktop background to rotate through some of these photos.

This is very easy to achieve with Gnome 2 – and is implemented in the Cosmos transitioning wallpaper. So on the Monday, I copied a bunch of resized photos into a folder under /usr/share/backgrounds, copied the XML file that powers Cosmos into the same folder, tweaked it, and was rather pleased to find my desktop wallpaper smoothly transitioning from one photo to the next.

On Tuesday, I added a few more photos and amended the XML file accordingly. By Wednesday, I was bored of manually editing the XML and started thinking about how best to automate this.

I have finally found a bit of time to do something about this and am now ready to release version 0.1 of Macsen’s Transitioning Background.

It’s probably not the most elegant solution (there is a lot of copying and pasting going on in the build() function) and the resulting XML file is not pretty. But it works.

You can find a tarball and some instructions on the MTB Project Page or browse the source at GitHub.