使用AppleScript旋转显示器(Monterey系统):脚本与设置窗口错误——如何点击"Roatation:"按钮?
在macOS Monterey用AppleScript点击显示器设置里的"Rotation:"按钮
我刚好折腾过类似的需求,在macOS Monterey环境下,要通过AppleScript点击显示器设置里的Rotation:按钮,关键是要精准定位系统设置里的UI元素,还要确保脚本有足够的权限。下面是亲测可行的方案:
第一步:给脚本开启必要权限
首先得给运行脚本的工具(比如脚本编辑器、Automator)和「系统设置」开启辅助功能权限,不然脚本会因为权限不足报错:
- 打开「系统设置」→「隐私与安全性」→「辅助功能」
- 在列表里勾选你的脚本运行工具(比如「脚本编辑器」)和「系统设置」
第二步:AppleScript代码实现
下面的脚本会自动打开系统设置的显示器面板,然后定位并点击Rotation:按钮。注意根据你的系统语言调整按钮文本:
-- 激活并导航到显示器设置面板 tell application "System Settings" activate reveal pane id "com.apple.Displays-Settings.extension" delay 1.5 -- 给窗口加载留缓冲时间,机器慢的话可以改成2秒 end tell -- 定位并点击Rotation按钮 tell application "System Events" tell process "System Settings" -- 英文系统用这行: set rotationButton to first button whose description contains "Rotation:" -- 中文系统替换成下面这行: -- set rotationButton to first button whose description contains "旋转:" -- 如果有多台显示器,先选中目标显示器的标签(可选) -- tell tab group 1 of window "Displays" -- click radio button 1 -- 1代表第一个显示器,按需调整 -- end tell click rotationButton end tell end tell
一些排查小技巧
如果脚本找不到按钮,可以用「辅助功能检查器」确认元素属性:
- 打开
/Applications/Utilities/Accessibility Inspector.app - 点击检查器窗口里的十字准星,拖到
Rotation:按钮上 - 查看元素的
description或name属性,把脚本里的判断条件改成匹配的内容
内容的提问来源于stack exchange,提问作者CJH
相关产品推荐
相关产品推荐

