macos - 通过系统偏好设置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以获得预期的结果?
解决方案
似乎存在系统偏好设置不正确的错误,因此,这是一种解决方法。reveal anchor "Switch" of pane id "com.apple.preference.universalaccess"
请注意,示例 AppleScript 代码中的编码假定切换控制以前已手动打开,以便回答初始系统偏好设置正在尝试解锁 您输入用户名和密码的辅助功能偏好设置对话框,然后单击解锁按钮.
示例 AppleScript 代码(如下所示)在macOS Catalina和macOS 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
适当设置。
推荐阅读
- c# - 确定极坐标图 Y 轴最大值的算法
- javascript - PhantomJS 没有足够的时间加载整个页面
- python - 寻找一个可以帮助我避免重复到文本文件中的函数
- c++ - 如何在 CCombobox::AddString() 中设置 unicode 文本而不用 unicode 编译整个 c++ 项目?
- javascript - 按字母顺序对页面上的元素进行排序
- hybris - 将表单数据从 Spartacus Storefront 发送到 Hybris 后端控制器
- c# - .NET Core 库未读取配置文件
- json - JSON模式验证数组的头部和尾部
- firebase - 如何从 Firebase 获取随机孩子
- sql-server - 如何记录存储过程执行运行时间