python 使用PyObjC和NSWorkspace设置桌面图片。我是个伪君子!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用PyObjC和NSWorkspace设置桌面图片。我是个伪君子!相关的知识,希望对你有一定的参考价值。

#!/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)

以上是关于python 使用PyObjC和NSWorkspace设置桌面图片。我是个伪君子!的主要内容,如果未能解决你的问题,请参考以下文章

使用 Esky 冻结/打包 Cocoa PyObjC Python 应用程序

python pyobjc的例子。

python macOS PyObjc Computername

python PyObjc系统Icon.py

python pyobjc:获取finder的可见窗口路径

macOS安装pyAutoGUI