我不擅长 Python,目前我使用的脚本是基于https://gist.github.com/gregneagle/6957826。
我希望对以下脚本中的桌面图像字典键选项进行一项更改,但我不确定正确的代码是什么。
当前代码
options = {}
我想要获得的是“NSWorkspaceDesktopImageAllowClippingKey”的 NO 值(参考:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/index.html#//apple_ref/doc/constant_group/Desktop_Image_Dictionary_Keys)
我的最终目标是让这个程序将 10.9 和 10.10 中的桌面图片设置为“适合屏幕”,而不是“填满屏幕”(它似乎总是默认设置为“填满屏幕”)。它是 NetRestore 图像实用程序的一部分,所以我需要自动执行此操作,因为该信息包含在 ByHost 首选项中。
谢谢你!
-rks
对于需要的人,这是原始脚本:
#!/usr/bin/python
'''Uses Cocoa classes via PyObjC to set a desktop picture on all screens.
Tested on Mountain Lion and Mavericks. Inspired by Greg Neagle's work: https://gist.github.com/gregneagle/6957826
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
'''
from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL
import argparse
import sys
parser = argparse.ArgumentParser(description='Sets the desktop picture on all screens')
parser.add_argument('--path', help='The path of the image')
args = vars(parser.parse_args())
if args['path']:
picture_path = args['path']
else:
print >> sys.stderr, 'You must supply a path for the desktop picture'
exit(-1)
# 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)
if error:
print error
exit(-1)