#! /usr/bin/env python from optparse import OptionParser import os import sys, string, time import random def changeWallpaper(filename): cmd = string.join(["gconftool -s /desktop/gnome/background/picture_filename -t string \"",filename,"\""],'') os.system(cmd) # execute system command # Parse commandline arguments parser = OptionParser() parser.add_option("-f", "--folder", dest="folder", help="use wallpapers from FOLDER", metavar="FOLDER") parser.add_option("-i", "--interval", type="float", dest="interval", default=300, help="time interval in seconds", metavar="INTERVAL") parser.add_option("-m", "--mode", type="string", dest="wallpaper_mode", default="centered", help="Wallpaper can be displayed in several modes: none, wallpaper, centered, scaled, stretched, zoom") parser.add_option("-d", "--default", dest="default_image", help="specify default image when program is killed", metavar="FILENAME", default="nothing.gif") parser.add_option("-c", "--bg-color-mode", dest="bg_color_mode", default="solid", help="Background Color Options: solid, hg (horizontal gradient, vg (vertical gradient)",metavar="COLOR_MODE") (options, args) = parser.parse_args() # Validate for presence of valid folder if options.folder is None: raise ValueError, "No valid folder specified" if not(os.path.isdir(options.folder)): raise ValueError, "The folder specified is not valid" # Set Background Color Mode if options.bg_color_mode == "vg": cmd = "gconftool -s /desktop/gnome/background/color_shading_type -t string \"vertical-gradient\"" os.system(cmd) # execute system command elif options.bg_color_mode == "hg": cmd = "gconftool -s /desktop/gnome/background/color_shading_type -t string \"horizontal-gradient\"" os.system(cmd) # execute system command else: cmd = "gconftool -s /desktop/gnome/background/color_shading_type -t string \"solid\"" os.system(cmd) # execute system command # Set wallpaper display mode cmd = string.join(["gconftool -s /desktop/gnome/background/picture_options -t string \"", options.wallpaper_mode ,"\""],'') os.system(cmd) # execute system command # Make list of images imagefilelist = [filename for filename in os.listdir(options.folder) if (filename.lower().endswith("jpg") or filename.lower().endswith("png") or filename.lower().endswith("gif"))] imagefilelist = [string.join([os.path.normpath(options.folder),imgFile],"/") for imgFile in imagefilelist] # Validate for presence of images if (len(imagefilelist) == 0): raise ValueError, "Folder does not contain any image files" count=0 #init index random.shuffle(imagefilelist) #randomize wallpaper order try: while (True): if (count < len(imagefilelist) -1): count = count + 1 else: count = 0 time.sleep(options.interval) changeWallpaper(imagefilelist[count]) except KeyboardInterrupt: print options.default_image cmd = string.join(["gconftool -s /desktop/gnome/background/picture_filename -t string \"", options.default_image ,"\""],'') os.system(cmd) # execute system command pass