]> code.delx.au - bg-scripts/blobdiff - lib/WallChanger.py
Moved randombg2 to a sensiable name
[bg-scripts] / lib / WallChanger.py
index 1281add7c2e744aa18bcc9684c49ad8b648fed1e..b7a81ab18fea42e341466fe084f4e5979f9fb682 100644 (file)
@@ -127,6 +127,8 @@ class __WMakerChanger(__BaseChanger):
 
 class __OSXChanger(__BaseChanger):
        _ConvertedWallpaperLocation = '/tmp/wallpapers/'
+       _DesktopPlistLocation = os.path.expanduser('~/Library/Preferences/com.apple.desktop.plist')
+
        def _removeOldImageCache(self):
                """Cleans up any old temp images"""
                if not os.path.isdir(self._ConvertedWallpaperLocation):
@@ -141,15 +143,39 @@ class __OSXChanger(__BaseChanger):
                """Convert the image to a png, and store it in a local place"""
                self._removeOldImageCache()
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
-               cmd = ["convert", file, output_name]
-               debug("""Convert command: '"%s"'""" % '" "'.join(cmd), DEBUG_LEVEL_DEBUG)
-               return output_name, subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=None).wait()
+               try:
+                       import PIL, PIL.Image
+                       img = PIL.Image.open(file)
+                       img.save(output_name, "PNG")
+                       return output_name, True
+               except ImportError:
+                       debug('Could not load PIL, going to try just copying the image')
+                       import shutil
+                       output_name = os.path.join(self._ConvertedWallpaperLocation, os.path.basename(file))
+                       shutil.copyfile(file, output_name)
+                       return output_name, True
+
+       def _fixDesktopPList(self):
+               """Removes the entry in the desktop plist file that specifies the wallpaper for each monitor"""
+               try:
+                       import Foundation
+                       desktopPList = Foundation.NSMutableDictionary.dictionaryWithContentsOfFile_(self._DesktopPlistLocation)
+                       # Remove all but the 'default' entry
+                       for k in desktopPList['Background'].keys():
+                               if k == 'default':
+                                       continue
+                               desktopPList['Background'].removeObjectForKey_(k)
+                       # Store the plist again (Make sure we write it out atomically -- Don't want to break finder)
+                       desktopPList.writeToFile_atomically_(self._DesktopPlistLocation, True)
+               except ImportError:
+                       debug('Could not import the Foundation module, you may have problems with dual screens', DEBUG_LEVEL_MEDIUM)
 
        def changeTo(self, file):
                output_name, ret = self._convertImageFormat(file)
-               if ret: # Since 0 indicates success
-                       debug("Convert failed %s" % ret)
+               if not ret:
+                       debug("Convert failed")
                        return False
+               self._fixDesktopPList()
                cmd = """osascript -e 'tell application "finder" to set desktop picture to posix file "%s"'""" % output_name
                debug(cmd, DEBUG_LEVEL_DEBUG)
                return not commands.getstatusoutput(cmd)[0]