Skip to content

Instantly share code, notes, and snippets.

@bryanzak
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanzak/9180980 to your computer and use it in GitHub Desktop.
Save bryanzak/9180980 to your computer and use it in GitHub Desktop.
Mavericks does not respect MCX applied to com.apple.desktop.plist. This script - which can be called via a LaunchAgent for every user that logs in - will read the plist file and extract the desktop image set by MCX. Normally this key is found in Background > default > ImageFilePath but sometimes (perhaps related to not being managed? or Mission …
#!/usr/bin/env python
# 1.1 2014-03-04 BP rewrote to use complete.plist MCX since the pref file method was unreliable
# 1.0 2014-02-23 BP first release for use with 10.9.x base images
# based on gist published by Greg Neagle: https://gist.github.com/gregneagle/6957826
# only needed (and tested) on 10.9, but may work on older OS versions
from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL
import getpass
import CoreFoundation
#import subprocess, sys, plistlib, json
import plistlib
def SetDesktop(imagepath):
file_url = NSURL.fileURLWithPath_(imagepath)
options = {} # empty options dict is ok because the defaults are ok
ws = NSWorkspace.sharedWorkspace()
for screen in NSScreen.screens():
(result, error) = ws.setDesktopImageURL_forScreen_options_error_(file_url, screen, options, None)
return;
def GetDesktopImagePathFromCompleteMCX():
path=""
mcxpath="/Library/Managed Preferences/" + username + "/complete.plist"
pl = plistlib.readPlist(mcxpath)
if pl:
key_desktop = pl.get('com.apple.desktop')
if key_desktop:
key_background = key_desktop.get('Background')
if key_background:
key_value = key_background.get('value')
if key_value:
key_default = key_value.get('default')
if key_default:
path = key_default.get('ImageFilePath')
if path:
print "success! managed desktop background key found: " + path
# can't find the correct key, no way to recover so use a hard coded fallback path
if path == "":
print "### ERROR: unable to find managed desktop background key - defaulting to Ladybug"
path="/Library/Desktop Pictures/Nature/Ladybug.jpg"
return path
# student accounts always start with an 's' so we can test for that
username=getpass.getuser()
if username[0] == 's':
imagePath=GetDesktopImagePathFromCompleteMCX()
print "[" + username + "] starts with an s, setting desktop picture to %s" % imagePath
SetDesktop(imagePath)
else:
print "[" + username + "] does not start with an s, desktop unchanged"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment