Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using PyObjC and NSWorkspace to set the desktop picture. I am such a hypocrite!
#!/usr/bin/python
'''Uses Cocoa classes via PyObjC to set a random desktop picture on all screens.
Tested on Mountain Lion and Mavericks.
See:
https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWorkspace_Class/Reference/Reference.html
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html
https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSScreen_Class/Reference/Reference.html
'''
import glob
import random
from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL
pictures_glob = glob.glob("/Library/Desktop Pictures/*.jpg")
picture_path = random.choice(pictures_glob)
# generate a fileURL for the desktop picture
file_url = NSURL.fileURLWithPath_(picture_path)
# make image options dictionary
# we just make an empty one because the defaults are fine
options = {}
# get shared workspace
ws = NSWorkspace.sharedWorkspace()
# iterate over all screens
for screen in NSScreen.screens():
# tell the workspace to set the desktop picture
(result, error) = ws.setDesktopImageURL_forScreen_options_error_(
file_url, screen, options, None)
@gregneagle

This comment has been minimized.

Copy link
Owner Author

gregneagle commented Oct 13, 2013

And that is why PyObjC is great.

@bryanzak

This comment has been minimized.

Copy link

bryanzak commented Oct 13, 2013

Not sure if this is supposed to work on 10.6.8 or not, but...

Traceback (most recent call last):
File "./tmp/SetDesktopPictures.py", line 8, in
from AppKit import NSWorkspaceDesktopImageScalingKey
ImportError: cannot import name NSWorkspaceDesktopImageScalingKey

@gregneagle

This comment has been minimized.

Copy link
Owner Author

gregneagle commented Oct 14, 2013

Not tested on Snow Leopard. Feel free to modify to work on SL!

@nriley

This comment has been minimized.

Copy link

nriley commented Oct 18, 2013

The API is all there in 10.6, but it looks like there are some issues with how the version of PyObjC bundled with 10.6 expose it.

#!/usr/bin/python

# https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWorkspace_Class/Reference/Reference.html
# https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html
# https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSScreen_Class/Reference/Reference.html

from AppKit import NSWorkspace, NSScreen
try:
    from AppKit import NSWorkspaceDesktopImageScalingKey
except ImportError:
    NSWorkspaceDesktopImageScalingKey = u'NSWorkspaceDesktopImageScalingKey'
from AppKit import NSImageScaleProportionallyUpOrDown
try:
    from AppKit import NSWorkspaceDesktopImageAllowClippingKey
except ImportError:
    NSWorkspaceDesktopImageAllowClippingKey = u'NSWorkspaceDesktopImageAllowClippingKey'
from Foundation import NSURL

picture_path = "/Library/Desktop Pictures/Tiles Blue.jpg"

# generate a fileURL
file_url = NSURL.fileURLWithPath_(picture_path)

# make image options dictionary
options = {
    NSWorkspaceDesktopImageScalingKey: NSImageScaleProportionallyUpOrDown,
    NSWorkspaceDesktopImageAllowClippingKey: True
}

# get shared workspace
ws = NSWorkspace.sharedWorkspace()

# iterate over all screens
for screen in NSScreen.screens():
    # tell the workspace to set the desktop picture
    # on 10.6 this returns 0 (failure) or 1 (success)
    # on 10.8 this returns a tuple (result, error); success is (True, None)
    result = ws.setDesktopImageURL_forScreen_options_error_(
        file_url, screen, options, None)
@dankeller

This comment has been minimized.

Copy link

dankeller commented Nov 13, 2013

Tested on Lion. Works great. Thanks, Greg!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.