Mac Sonoma下AppleScript选择Office音箱失败:仅打开声音设置界面
修复Mac Sonoma 14.3.1下AppleScript切换输出音箱的问题
原代码卡在打开声音设置界面的核心原因是Mac Sonoma已经把旧的「系统偏好设置(System Preferences)」替换成了「系统设置(System Settings)」,原代码仍在引用旧进程名,导致后续UI操作找不到目标元素。另外Sonoma的系统设置UI结构也有变化,需要调整元素定位逻辑。
修正后的代码如下:
tell application "System Settings" activate reveal pane id "com.apple.Sound-Settings.extension" delay 0.3 -- 给界面加载留缓冲时间 end tell tell application "System Events" tell process "System Settings" -- 等待Sound设置窗口加载完成 repeat until exists window "Sound" delay 0.5 end repeat tell window "Sound" -- 等待Output选项加载 repeat until exists radio button "Output" of group 1 delay 0.5 end repeat click radio button "Output" of group 1 -- 等待设备列表加载 repeat until exists table 1 of scroll area 1 of group 1 delay 0.5 end repeat tell table 1 of scroll area 1 of group 1 -- 定位到名为"Office"的设备行并点击 repeat with eachRow in rows if value of static text 1 of eachRow is "Office" then click eachRow exit repeat end if end repeat end tell end tell end tell end tell
关键修改点:
- 把System Events中操作的进程从
"System Preferences"改为"System Settings",匹配Sonoma的新应用 - 调整UI元素层级:Output选项现在属于
group 1而非原代码的tab group 1 - 替换设备行查找逻辑:用循环遍历行,直接匹配目标设备名后点击,避免原代码中
position属性调用的错误 - 增加短延迟,确保界面元素完全加载后再执行操作
内容的提问来源于stack exchange,提问作者JSA986
相关产品推荐
相关产品推荐

