調整10.10桌面更改Python腳本

調整10.10桌面更改Python腳本

我對 Python 不太擅長,我目前使用的腳本是基於https://gist.github.com/gregneagle/6957826

我希望對以下腳本中的桌面圖像字典鍵選項進行一項更改,但我不確定正確的程式碼是什麼。

目前程式碼

options = {}

我想要獲得的“NSWorkspaceDesktopImageAllowClippingKey”沒有值(參考: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 Preferences 中。

謝謝你!

-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)

相關內容