The version of Gnome in Ubuntu 10.04 has the ability to automatically change through a defined set of files. Unfortunately, there’s no simple way to do this via the gui (I originally assumed that you just assigned a folder to be your background) so I whipped up a quick Python script. This could probably have been done in bash, but I took the lazy way out. I’m also sure I could have used some existing XML library, but the file is fairly simple, so I just used file.write(). If any glaring errors are pointed out, or if I feel like implementing XML properly, I’m sure I’ll post a revised version sometime soon, but for now, here’s the program:
Here’s a link to the source, in case WordPress fubars Python’s indentation.
#!/usr/bin/python
import os, os.path, sys, optparse
IMG_FILETYPES = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg']
def gen_bg_xml(directory, duration=1795.0, transition=5.0):
if not os.path.isdir(directory):
print “Error: {0} is not a valid directory”.format(directory)
sys.exit(2)xmlfile = os.path.join(directory, “background.xml”)
xml = open(xmlfile, mode=’w')xml.write(“<background>\n”)
xml.write(“ <starttime>\n”)
xml.write(“ <year>2010</year>\n”)
xml.write(“ <month>01</month>\n”)
xml.write(“ <day>01</day>\n”)
xml.write(“ <hour>00</hour>\n”)
xml.write(“ <minute>00</minute>\n”)
xml.write(“ <second>00</second>\n”)
xml.write(“ </starttime>\n”)files = os.listdir(directory)
l = files[:] # Copy the list so we have something to iterate through
for f in l:
name, ext = os.path.splitext(f)
if not ext in IMG_FILETYPES:
files.remove(f)
continuefor f in files:
absf = os.path.join(directory, f)
if not files.index(f) == 0:
xml.write(“ <to>{0}</to>\n”.format(absf))
xml.write(“ </transition>\n”)
xml.write(“ <static>\n”)
xml.write(“ <duration>{0}</duration>\n”.format(duration))
xml.write(“ <file>{0}</file>\n”.format(absf))
xml.write(“ </static>\n”)
xml.write(“ <transition>\n”)
xml.write(“ <duration>{0}</duration>\n”.format(transition))
xml.write(“ <from>{0}</from>\n”.format(absf))
if files.index(f) == len(files) – 1:
xml.write(“ <to>{0}</to>\n”.format(
os.path.join(directory, files[0])))
xml.write(“ </transition>\n”)
xml.write(“</background>”)
xml.flush()
xml.close()if __name__==”__main__”:
p = optparse.OptionParser(usage=”usage: %prog [options] directory”)
p.add_option(‘–duration’, ‘-d’, default=”1795.0″)
p.add_option(‘–transition’, ‘-t’, default=”5.0″)
options, arguments = p.parse_args()
if not len(arguments) == 1:
p.error(“incorrect number of arguments”)
sys.exit(1)
gen_bg_xml(arguments[0], options.duration, options.transition)
Whew, escaping all those tabs was annoying.
Thanks for posting this. This script was just what I was looking for.
If I add more files and rerun the script, do you know if Gnome will pick up the change in the background automatically?
Eric
Pingback: Dragonsept Arts & Publishing Blog » Blog Archive » Creating a rotating wallpaper “stack” for your Ubuntu 10.04 (and other Gnome 2.28) system
yes, I actually have it running in a cronjob every 10 minutes
Thanks Zach, this worked beautifully and saved me from scripting it myself!
Pingback: Rotating Gnome backgrounds « The Ubuntu Incident