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

使用AppleScript修改设置/系统首选项,applescript,Applescript,我正在尝试制作一个AppleScript,用于切换空间的自动重新排列。我能够获得AppleScript以打开系统首选项并进入任务控制设置,但是我不确定如何选中要更改的框 tell application "System Preferences" activate end tell tell application "System Events" tell process "System Preferences" click menu item "Miss

我正在尝试制作一个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

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

在本例中,
根据最近的使用情况自动重新排列空间
是您要选中的复选框

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
如果你只想在未检查的情况下检查:

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
如果要列出窗口中的所有元素:

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

这是一种通过AppleScript使用shell命令的替代方法,其优点是不要求系统首选项处于打开/运行/可见状态。速度也快得多

如果您碰巧打开了它以监视下面的脚本是否工作,请记住,GUI(您看到的)不会更新,直到您关闭系统首选项,然后再次打开它

要将“根据最近使用情况自动重新排列空间”设置为true(即勾选复选框),请执行以下操作:

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

要切换设置,此短脚本将实现以下功能:

    set currentSetting to ¬
        (do shell script ¬
            "defaults read com.apple.dock 'mru-spaces'") ¬
            as integer as boolean

    do shell script ¬
        "defaults write com.apple.dock 'mru-spaces' -bool " & ¬
        (not currentSetting) & ¬
        "; killall Dock"
注意:使用MacOS High Sierra进行了测试,但(我相信)在OSX Mavericks和更高版本中应该可以使用


其他设置 切换到应用程序时,切换到应用程序窗口打开的空间

(或
false
如果要关闭该选项。)

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


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

首先让我说,虽然在此之前的两个答案都有效,但出于以下原因,我不会使用其中任何一个

shadowsheep给出的答案是有效的,但是它不必要地暴露了系统首选项GUI,我相信除非您的系统真的很慢,
delay
命令的值超过50%,在这个用例中应该只需要一个

CJK给出的答案是可行的,但是它使用了
killall Dock
,这会在视觉上分散注意力,导致所有桌面上所有最小化的窗口都无法缩小,从而导致进一步的视觉分散,并使桌面变得凌乱,这可能需要用户清理混乱。即使没有其他窗口打开,它仍然是一种视觉干扰,而不是我将展示的内容

现在,每个用户都有不同的工作习惯,所以可能上面提到的任何原因都不会对您产生任何影响。就我个人而言,我在四个虚拟桌面之间工作,可以在桌面上的许多应用程序中打开几十个窗口,其中许多窗口(如果不是大多数的话)有时被最小化。为我使用
killall Dock
是我大多数时候最不想做的事情

话虽如此,这里有一个替代现有答案的方法

可以肯定地说,大多数用户不会打开或保持打开的系统首选项但是下面的示例AppleScript代码会检查它是否正在运行,以及是否正在关闭它。这样就可以在不显示GUI的情况下打开它,这样就不必看到随着脚本的进行而更改GUI的视觉干扰

此示例AppleScript代码仅切换目标复选框的状态:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            click checkbox "Automatically rearrange Spaces based on most recent use"
        end tell
    end tell
    quit
end tell
此示例AppleScriptcode使用
中的
0
1
有条件地单击目标复选框,如果值等于0,则单击该复选框。使用
0
仅在未选中时单击它,使用
1
仅在选中时单击它

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            tell checkbox "Automatically rearrange Spaces based on most recent use"
                if value is equal to 0 then click it
            end tell
        end tell
    end tell
    quit
end tell
显示的两个示例AppleScript代码块工作速度都很快,而且看不到系统首选项GUI,唯一的视觉效果是系统首选项停靠磁贴,它只会反弹一次,甚至可能不明显,尤其是与killall Dock的视觉分散相比


请注意,
delay
命令的值可能需要针对您的系统进行调整,或者可能需要或不需要额外的
delay
命令。根据需要调整和或添加/删除
延迟
命令的值



注意:示例AppleScript代码就是这样,没有使用任何其他错误处理,只是为了显示完成任务的多种方法之一。用户始终有责任根据需要/需要添加/使用适当的错误处理

@AlexStedman不客气!但是您是否忘记将其标记为正确答案?;)你是这样说的吗<代码>将myvar设置为“告诉应用程序[…]告诉窗口”应用程序进程的“任务控制”系统首选项将myvar设置为获取整个内容结束告诉[…]
我可以,但它更混乱。无论如何,我更愿意建议一种迭代所有元素的方法。如果您愿意,还可以使用可访问性检查器检查层次结构。顺便提一下对于一个只问了很少的问题,甚至没有选择正确答案的问题,我们需要更多的信息;)@CJK我忘了在我之前的评论中标记你;)@很抱歉。我是stack exchange新手,不知道应该选择正确的答案。似乎很明显now@CJK,你说,“实际上,我的答案中的一个设置不需要killall Dock”,那么OP的主要焦点是切换“根据最近使用情况自动重新排列空间”复选框以及答案中t的部分
do shell script "defaults write com.apple.dock 'expose-group-apps' -bool true; killall Dock"
if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            click checkbox "Automatically rearrange Spaces based on most recent use"
        end tell
    end tell
    quit
end tell
if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            tell checkbox "Automatically rearrange Spaces based on most recent use"
                if value is equal to 0 then click it
            end tell
        end tell
    end tell
    quit
end tell