16 May 2012 @ 4:06 PM 

One of the most aggravating things when I switched to Linux Mint 12 from Mint 10 was that it seemed to offer a step backwards for a lot of functionality. I was using a program called “Desktop Drapes” to periodically cycle my wallpaper, but it no longer worked in Gnome 3. (there were some supposed plugins to fix this but they didn’t work). I tried a few others, but they universally failed.

 

So I ended up writing a python script to do the job:

 

  1.  
  2. #!/usr/bin/python
  3.  #Copyright (c) 2011 BASeCamp Corporation
  4.  # All rights reserved.
  5.  #
  6.  # Redistribution and use in source and binary forms, with or without
  7.  # modification, are permitted provided that the following conditions
  8.  # are met:
  9.  #
  10.  # Redistributions of source code must retain the above copyright notice,
  11.  # this list of conditions and the following disclaimer.
  12.  #
  13.  # Redistributions in binary form must reproduce the above copyright
  14.  # notice, this list of conditions and the following disclaimer in the
  15.  # documentation and/or other materials provided with the distribution.
  16.  #
  17.  # Neither the name of the project’s author nor the names of its
  18.  # contributors may be used to endorse or promote products derived from
  19.  # this software without specific prior written permission.
  20.  #
  21.  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22.  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23.  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24.  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25.  # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26.  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27.  # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28.  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29.  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30.  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.  
  34.  
  35. import sys,os,random,ConfigParser,string
  36. from optparse import OptionParser
  37. #Wallpaper cycling script
  38. #written Sunday March 18th 2012
  39. #designed to be added to crontab with a delay
  40. #example line in crontab:
  41. #*/4 * * * * /home/bc_programming/bin/cyclebg.py
  42. #when run, it will change the wallpaper to the next one in the cycle and exit.
  43. #Author: BC_Programming
  44.  
  45.  
  46.  
  47. #location of data path. we keep track of the wallpapers we have already cycled there.
  48. datapath = os.path.expanduser("~/.cyclebg/")
  49. datafile = datapath + "cycled_data.dat"
  50. wallpaperfile = datapath + "wallpapers.dat"
  51. disablefile = datapath + "disable.dat" #if this file exists we are "disabled" and don’t change the wallpaper.
  52. #command to run to change the bg.
  53.  
  54. #following is for Gnome 2
  55. #bgchangecmd = ‘dconf gsettings set org.gnome.desktop.background picture-uri "file://%s"’
  56. #gnome 3 command…
  57.  
  58. bgchangecmd = ‘gsettings set org.gnome.desktop.background picture-uri "file:///%s"’
  59. #we need two lists: the list of possible wallpapers, and the wallpapers we’ve already listed.
  60.  
  61. def isdisabled():
  62.    return os.path.exists(disablefile)
  63. #returns the list of wallpapers we’ve already cycled through.
  64. def getusedlist():
  65.     if not os.path.exists(datafile):
  66.         #if the file doesn’t exist, then return an empty list.
  67.         return  []
  68.     else:
  69.         return list(open(datafile))
  70.  
  71. #clears the used wallpapers list.
  72. def clearused():
  73.     if os.path.exists(datafile):
  74.        os.remove(datafile)
  75.  
  76. def markused(wallpaper):
  77.     #mark a wallpaper as viewed
  78.     writelist = open(datafile,‘a’)
  79.     writelist.write(wallpaper)
  80.  
  81. #returns all the wallpapers we are to cycle through.
  82. def getwallpapers():
  83.     if not os.path.exists(wallpaperfile):
  84.        return  []
  85.     else:
  86.        return list(open(wallpaperfile))
  87.  
  88.  
  89.  
  90. #routine that changes the background.
  91. def changebackground(picture):
  92.     #changes the background to the specified image.
  93.     cmdrun =bgchangecmd
  94.     cmdrun = cmdrun.replace("%s",picture)
  95.     print " running ",cmdrun
  96.     os.system(cmdrun)    
  97.  
  98. def main():
  99.     if isdisabled(): sys.exit(2)
  100.     sys.exit(changewallpaper())
  101.  
  102. def changewallpaper():
  103.     #retrieve the wallpapers and used wallpapers…
  104.     used = getusedlist()
  105.     papers = getwallpapers()
  106.     #exit if no wallpapers in main list…
  107.     if len(papers)==0:
  108.         print "No wallpapers! add wallpaper file paths to ", wallpaperfile, "!"
  109.         return 1
  110.        
  111.     #now, remove all entries in papers that exist in used.
  112.     removethese= []  #list of items to remove
  113.     for iterateused in papers:
  114.         if iterateused in used:
  115.            removethese = removethese +  [iterateused]
  116.        
  117.  
  118.     #now, remove the items…
  119.     for iterateremove in removethese:
  120.         papers.remove(iterateremove)
  121.  
  122.  
  123.     if len(papers)==0:
  124.         #if papers list is empty, egads! clear the used list, since we’ve cycled through them all evidently.
  125.         clearused()
  126.         #and… well balls to it we’ll just call ourself.
  127.         changewallpaper()
  128.  
  129.  
  130.  
  131.     #now, the main logic. Choose a wallpaper…
  132.     choosefrom = papers
  133.     #shuffle the choosefrom listing…
  134.     random.shuffle(choosefrom)
  135.     #take one of the items from the resulting shuffled list.
  136.     chosen = choosefrom.pop()
  137.     #add it to the list of seen wallpapers…
  138.     markused(chosen)
  139.     #and attempt to change to it…
  140.     changebackground(chosen)        
  141.     return 0
  142.  
  143.  
  144.  
  145. #parse our options/arguments…
  146. parser = OptionParser(usage="usage: %prog  [options]  ",
  147.                       version="%prog 1.0")
  148. parser.add_option("-g","–gui",
  149. action="store_true", dest="gui", default=False,
  150. help="run GUI")
  151.  
  152. global options,args
  153. (options,args) = parser.parse_args();
  154.  
  155.  
  156.  
  157.  
  158. main()
  159.  
  160.  
  161.  

The script is designed to be used by adding a like to crontab.

1,712 total views, 4 views today

Have something to say about this post? Comment!

Posted By: BC_Programming
Last Edit: 16 May 2012 @ 04:06 PM

EmailPermalink
Tags
Categories: Programming


 

Responses to this post » (One Total)

 
  1. Baggerboot says:

    So this is the background changer script you’ve been talking about earlier? Interesting.

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 Last 50 Posts
 Back
Change Theme...
  • Users » 871
  • Posts/Pages » 193
  • Comments » 69
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

PP



    No Child Pages.

Windows optimization tips



    No Child Pages.