使用 AppleScript 修改设置/系统首选项

Ale*_*man 7 applescript

我正在尝试制作一个可以切换空间自动重新排列的 AppleScript。我能够让 AppleScript 打开系统首选项并进入任务控制设置,但是我不确定如何选中我想要更改的框。

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Mission Control" of menu "View" of menu bar 1
    delay 2
    tell window "Mission Control"
            //additional code goes here
    end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

有没有办法查看窗口的组件是什么,以便我知道在我能够访问切换设置的复选框之前是否需要进入表格或其他内容

CJK*_*CJK 7

这是通过 AppleScript 使用shell 命令的替代方法,其优点是不需要打开/运行/可见系统偏好设置。它也快得多。

\n

如果您碰巧打开它来监视下面的脚本是否有效,请记住,在关闭“系统偏好设置”并再次打开它之前,GUI(您所看到的)不会更新。

\n

设置根据最近使用情况自动重新排列空间为 true(即勾选复选框):

\n
do shell script "defaults write com.apple.dock 'mru-spaces' -bool true; killall Dock"\n
Run Code Online (Sandbox Code Playgroud)\n

要将其设置为 false,即取消选中该复选框,将上面的代码行更改true为。false

\n

要切换设置,这个简短的脚本将实现:

\n
    set currentSetting to \xc2\xac\n        (do shell script \xc2\xac\n            "defaults read com.apple.dock 'mru-spaces'") \xc2\xac\n            as integer as boolean\n\n    do shell script \xc2\xac\n        "defaults write com.apple.dock 'mru-spaces' -bool " & \xc2\xac\n        (not currentSetting) & \xc2\xac\n        "; killall Dock"\n
Run Code Online (Sandbox Code Playgroud)\n

注意:已使用 MacOS High Sierra 进行测试,但(我相信)应该可以在 OS X Mavericks 及更高版本中工作。

\n
\n

其他设置

\n

切换到应用程序时,切换到该应用程序打开窗口的空间

\n
do shell script "defaults write -g AppleSpacesSwitchOnActivate -bool true"\n
Run Code Online (Sandbox Code Playgroud)\n

(或者false如果您想关闭该选项。)

\n

按应用程序对窗口进行分组

\n
do shell script "defaults write com.apple.dock 'expose-group-apps' -bool true; killall Dock"\n
Run Code Online (Sandbox Code Playgroud)\n

(或者false如果您想关闭该选项。)

\n


sha*_*eep 1

这应该是你想要的。

在此示例中Automatically rearrange Spaces based on most recent use是您要检查的复选框。

tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        click checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
    end tell
    quit
end tell
Run Code Online (Sandbox Code Playgroud)

如果您只想在未检查时检查它:

tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        tell checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
            if (get its value) = 0 then click it
        end tell
    end tell
    quit
end tell
Run Code Online (Sandbox Code Playgroud)

如果你想列出窗口中的所有 UIElement:

set myArray to {}
tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        tell window "Mission Control" of application process "System Preferences"
            repeat with uiElem in entire contents as list
                set myArray to myArray & ((class of uiElem as string) & " : " & name of uiElem as string)
            end repeat
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)