applescript 通过系统偏好设置访问开关控制

applescript access switch control through system preferences

我的最终目标是单击“启用切换控制”复选框,该复选框位于“系统偏好设置”中的“辅助功能”->“切换控制”下。我在让 applescript 工作时遇到了一些麻烦,想找出导致问题的原因。 我当前的代码:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
    
tell application "System Preferences"
    reveal anchor "Switch" of pane id "com.apple.preference.universalaccess"
    activate
end tell

我通过使用获得了锚开关:

tell application "System Preferences"
    get anchors of pane id "com.apple.preference.universalaccess"
end tell

它返回了所有可用的锚点。这是一个片段:

...anchor TextToSpeech of pane id com.apple.preference.universalaccess, anchor Dwell of pane id com.apple.preference.universalaccess, anchor Dictation of pane id com.apple.preference.universalaccess, anchor Switch of pane id com.apple.preference.universalaccess, anchor Siri of pane id com.apple.preference.universalaccess...

我尝试使用其他锚点(听写、Siri 等)测试 applescript,它们都有效(将我定向到预期区域),但 Switch 没有。我什至尝试用它的数值 reveal anchor 11 of pane id "com.apple.preference.universalaccess" 替换“Switch”,但这也失败了。这是为什么?另外,我将如何修改 applescript 以获得预期的结果?

似乎有一个 bug System Preferences 将无法正常 reveal anchor "Switch" of pane id "com.apple.preference.universalaccess" 因此,这是一个解决方法。

请注意,示例 AppleScript code 中的编码假定 Switch Control 之前曾被手动打开以响应初始 System Preferences is trying to unlock Accessibility preferences 对话框 在您输入 用户名密码 的位置,然后单击 解锁 按钮。

示例 AppleScript 代码,如下所示,在Script EditormacOS CatalinamacOS Big SurLanguage & Region 设置在 系统偏好设置 中设置为 英语(美国)- 主要 并为我工作 click 启用切换控制 复选框1

  • 1 系统偏好设置 > 安全和隐私[= 中假定必要且适当的设置70=] > 隐私 已根据需要set/addressed。

并为我工作 单击 启用切换控制 复选框:

--  # Check to see if System Preferences is 
--  # running and if yes, then close it.
--  # 
--  # This is done so the script will not fail 
--  # if it is running and a modal sheet is 
--  # showing, hence the use of 'killall' 
--  # as 'quit' fails when done so, if it is.
--  #
--  # This is also done to allow default behaviors
--  # to be predictable from a clean occurrence.

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
    delay 0.1
end if

--  # Make sure System Preferences is not running before
--  # opening it again. Otherwise there can be an issue
--  # when trying to reopen it while it's actually closing.

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

--  # Open System Preferences to the Accessibility pane.

tell application "System Preferences" to ¬
    reveal pane id "com.apple.preference.universalaccess"

--  # Use System Events to achieve the goal.

tell application "System Events"
    tell window 1 of application process "System Preferences"
        
        --  # Wait for target pane to be available.
        --  # The target in this case has a checkbox.
        
        my waitForUIelement(checkbox 1)
        
        --  # Ascertain the target row to select.
        
        set rowSwitchControl to the first row of ¬
            table 1 of scroll area 1 whose value of ¬
            static text 1 of UI element 1 is ¬
            "Switch Control"
        
        --  # Select the target row.
        
        select rowSwitchControl
        
        --  # Wait for target checkbox to be available.
        
        my waitForUIelement(checkbox 1 of tab group 1 of group 1)
        
        --  # Click the Enable Switch Control checkbox.
        
        click checkbox 1 of tab group 1 of group 1
        
    end tell
end tell

delay 0.02

quit application "System Preferences"


--  ## Handler(s) ##

on waitForUIelement(uiElement)
    tell application "System Events"
        tell window 1 of application process ¬
            "System Preferences"
            set i to 0
            repeat until exists uiElement
                delay 0.1
                set i to i + 1
                if i ≥ 30 then return
            end repeat
        end tell
    end tell
end waitForUIelement

注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任添加任何适当、需要或想要的 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟