【问题标题】:applescript access switch control through system preferences通过系统偏好设置applescript访问开关控制
【发布时间】:2021-03-15 20:42:56
【问题描述】:

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

它返回了所有可用的锚点。这是一个sn-p:

...窗格 ID com.apple.preference.universalaccess 的锚 TextToSpeech,窗格 ID com.apple.preference.universalaccess 的锚 Dwell,锚 窗格 ID com.apple.preference.universalaccess 的听写,窗格 ID com.apple.preference.universalaccess 的锚点 切换,窗格 ID com 的锚点 Siri。 apple.preference.universalaccess...

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

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    似乎有一个 错误,即 系统偏好设置 不能正确地reveal anchor "Switch" of pane id "com.apple.preference.universalaccess",因此,这里有一个解决方法。

    请注意,示例 AppleScript 代码 中的编码假定 Switch Control 之前已打开过一次手动回答最初的System Preferences is trying to unlock Accessibility preferences 对话框您输入了用户名密码 然后点击解锁按钮。

    示例 AppleScript 代码,如下所示,在 ma​​cOS Catalina 下的 Script Editor 中进行了测试ma​​cOS Big Sur系统偏好设置 中的 语言和地区 设置设置为 English (US) - Primary strong> 并为我工作,单击 启用开关控制 复选框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 Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 2021-08-26
      • 2016-09-15
      • 1970-01-01
      • 2016-05-12
      • 2021-05-28
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多