Last active
July 24, 2024 09:28
-
-
Save gregneagle/6957826 to your computer and use it in GitHub Desktop.
Using PyObjC and NSWorkspace to set the desktop picture. I am such a hypocrite!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
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
Not tested on Snow Leopard. Feel free to modify to work on SL!
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)
Tested on Lion. Works great. Thanks, Greg!
Hi, doesn't work with sonoma..., any help?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And that is why PyObjC is great.