Monday 25 February 2013

Automatic wallpaper changer for GNOME 3

So, I use Linux MINT 14 Cinnamon as my desktop at work, and I like having interesting wall papers as my background. Most of the programs designed to change wallpapers are for GNOME 2, and the few that were GNOME 3 missed out an important thing for me, that I have two monitors at work and wanted to display dual screen wallpapers. So, I wrote a quick little program to do it for me:


#!/usr/bin/python

#script to change wallpaper every 2 minutes.
#if single : gsettings set org.gnome.desktop.background picture-options zoom
#if dual : gsettings set org.gnome.desktop.background picture-options spanned
#To do, measure the picture size and make it dual or single based on proportions

single_monitor = '/home/andy-repton/Pictures/Wallpapers/Single Screen'
dual_monitor = '/home/andy-repton/Pictures/Wallpapers/Dual Screen'

import os
import subprocess

single_monitor_wallpapers = os.listdir(single_monitor)
dual_monitor_wallpapers = os.listdir(dual_monitor)
number_of_single = len(single_monitor_wallpapers) -1
number_of_dual = len(dual_monitor_wallpapers) - 1

from random import randint
single_dual = randint(0,1)
if single_dual == 0:
        number = randint(0,number_of_single)
        wallpaper = single_monitor_wallpapers[number]
        options = ['/usr/bin/gsettings', 'set', 'org.gnome.desktop.background', 'picture-options', 'scaled']
        call = subprocess.Popen(options, stdout=subprocess.PIPE).communicate()
        options = ['/usr/bin/gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file:///home/andy-repton/Pictures/Wallpapers/Single Screen/' + wallpaper]
        call = subprocess.Popen(options, stdout=subprocess.PIPE).communicate()

else:
        number = randint(0,number_of_dual)
        wallpaper = dual_monitor_wallpapers[number]
        options = ['/usr/bin/gsettings', 'set', 'org.gnome.desktop.background', 'picture-options', 'spanned']
        call = subprocess.Popen(options, stdout=subprocess.PIPE).communicate()
        options = ['/usr/bin/gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', 'file:///home/andy-repton/Pictures/Wallpapers/Dual Screen/' + wallpaper]
        call = subprocess.Popen(options, stdout=subprocess.PIPE).communicate()


It's written in python, and if you'd like to use it you'll need to edit the single_monitor and dual_monitor variables to point to your directories full of single and dual screen wallpapers respectively. The trick to making it span both screens is this line:
gsettings set org.gnome.desktop.background picture-options spanned
(If you were to call it from a terminal prompt you'd use that, but here I'm using the python module subprocess)

Annoyingly, you can't call this from cron, as it requires a TTY which cron doesn't have, so a nasty hack like this run in the background should work:

while true; do ./screen_changer.py; sleep 240; done

I've named my script 'screen_changer.py' here (make sure you chmod it executable) and told it to go to sleep for 4 minutes, so my wallpaper will change every 4 minutes.

Enjoy!

Andy

No comments:

Post a Comment