macOS Ventura中AppleScript无法触发语音控制开关的问题
macOS Ventura中AppleScript无法触发语音控制开关的解决方法
尝试用AppleScript在macOS Ventura中触发语音控制启用开关,脚本能正常打开语音控制设置页面,但始终无法点击开关,当前通过PyCharm的Python applescript模块运行脚本,原脚本如下:
do shell script "open -b com.apple.systempreferences " & ¬ "/System/Library/PreferencePanes/UniversalAccessPref.prefPane" tell application "System Events" tell its application process "System Settings" repeat until UI element 4 of group 1 of scroll area 1 of group 1 of ¬ group 2 of splitter group 1 of group 1 of window "Accessibility" exists delay 0.1 end repeat click UI element 1 of group 3 of scroll area 1 of group 1 of group 2 ¬ of splitter group 1 of group 1 of window "Accessibility" repeat until checkbox 3 of group 2 of scroll area 1 of group 1 of group ¬ 2 of splitter group 1 of group 1 of window "Voice Control" exists delay 0.1 end repeat click button 5 of group 1 of scroll area 1 of window "Voice Control" end tell end tell tell application "System Settings" to quit
问题原因
- UI元素索引不稳定:macOS Ventura改用
System Settings替代旧版System Preferences,界面结构动态性更强,依赖固定索引(如UI element 4、button 5)定位控件极易失效,索引会随界面布局、系统更新变化。 - 权限不足:
System Events或运行脚本的PyCharm未获得辅助功能权限,导致无法模拟点击操作。
解决步骤
1. 配置辅助功能权限
- 打开「系统设置」→「隐私与安全性」→「辅助功能」
- 检查并勾选
System Events和PyCharm(或运行脚本的Python环境)
2. 使用控件属性定位的修改版脚本
改用控件的name属性定位,稳定性更强,修改后的脚本如下:
-- 直接打开辅助功能设置面板 do shell script "open x-apple.systempreferences:com.apple.Accessibility-Settings.extension" tell application "System Events" tell application process "System Settings" -- 等待辅助功能页面加载,定位「语音控制」选项 repeat until exists row "Voice Control" of table 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window "Accessibility" delay 0.2 end repeat click row "Voice Control" of table 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window "Accessibility" -- 等待语音控制页面加载,定位启用开关 repeat until exists checkbox "Enable Voice Control" of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Voice Control" delay 0.2 end repeat click checkbox "Enable Voice Control" of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Voice Control" end tell end tell -- 退出系统设置 quit application "System Settings"
3. 额外优化提示
- 如果仍无法点击,可适当增加
delay时长,确保页面完全加载 - 运行脚本时避免操作鼠标/键盘,防止干扰UI自动化操作
内容的提问来源于stack exchange,提问作者BackTo2020
相关产品推荐
相关产品推荐

