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) |
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
Tested on Lion. Works great. Thanks, Greg!