Ventura系统下AppleScript切换辅助功能灰度显示失效,如何修改脚本?
解决Ventura系统下AppleScript切换灰度显示的问题
嘿,刚好碰到过这个问题!Ventura系统把系统设置的结构改了,老的AppleScript自然就失效了,不过有两个靠谱的解决办法,看你偏好哪种:
方法一:用底层配置命令(更稳定推荐)
这种方法直接修改系统的底层配置,不会受系统设置UI布局变化的影响,执行速度也更快:
tell application "System Events" set currentGrayscale to do shell script "defaults read com.apple.CoreGraphics DisplayUseGrayscale" if currentGrayscale is "0" then do shell script "defaults write com.apple.CoreGraphics DisplayUseGrayscale -bool true" else do shell script "defaults write com.apple.CoreGraphics DisplayUseGrayscale -bool false" end if -- 重启Dock让设置立即生效 do shell script "killall Dock" end tell
原理是通过defaults命令读写系统的灰度显示配置,最后重启Dock让设置立刻生效,不用手动刷新系统设置。
方法二:更新UI脚本路径(模拟手动操作)
如果你偏好模拟手动点击系统设置的方式,可以用这个更新后的脚本——Ventura把灰度设置移到了「辅助功能」的「显示」面板里,所以脚本需要调整UI元素的定位路径:
tell application "System Settings" activate -- 直接跳转到辅助功能的显示设置页面 reveal anchor "Display" of pane id "com.apple.preference.accessibility" delay 0.5 -- 给系统设置留一点加载时间,避免找不到元素 end tell tell application "System Events" tell application process "System Settings" -- 定位「使用灰度」复选框 set grayscaleCheckbox to checkbox "使用灰度" of group 1 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "辅助功能" -- 切换复选框状态 click grayscaleCheckbox end tell end tell -- 可选:自动关闭系统设置窗口 tell application "System Settings" to quit
⚠️ 重要注意事项:
- 不管用哪种方法,都需要给运行脚本的应用(比如Script Editor)在「系统设置→隐私与安全性→辅助功能」里授予权限,不然脚本会因权限不足执行失败。
- 方法二的UI路径可能会随系统小版本更新变化,如果未来失效,需要重新定位UI元素;方法一则几乎不会受系统更新影响。
内容的提问来源于stack exchange,提问作者ppt
相关产品推荐
相关产品推荐

