首页 > 解决方案 > 通过系统偏好设置applescript访问开关控制

问题描述

我的最终目标是单击“启用切换控制”复选框,该复选框可以在“系统偏好设置”的“辅助功能”->“切换控制”下找到。我在让 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,窗格 ID com.apple.preference.universalaccess 的锚Siri ...

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

标签: macosapplescript

解决方案


似乎存在系统偏好设置不正确的错误,因此,这是一种解决方法。reveal anchor "Switch" of pane id "com.apple.preference.universalaccess"

请注意,示例 AppleScript 代码中的编码假定切换控制以前已手动打开,以便回答初始系统偏好设置正在尝试解锁 您输入用户名密码的辅助功能偏好设置对话框,然后单击解锁按钮.

示例 AppleScript 代码(如下所示)在macOS CatalinamacOS Big Sur下的脚本编辑器中进行了测试,系统偏好设置中的语言和区域设置设置为英语(美国) - 主要并且对我来说毫无问题地单击启用切换控制复选框1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。

并为我单击启用开关控制 复选框

--  # 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 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。


推荐阅读