有时候我用一些专业软件,或者玩游戏的时候需要用到键盘上的 F1~F12 那些快捷键。
但是平常我还是需要关掉,调节音量大小屏幕明暗的。
我目前自己用快捷指令创建了一个 Applescript ,用 AI 写的。但是这个快捷指令只能打开到开关 FN+F1~F2 这个界面,最后还是我要点击开关。
用了不同的 AI ,但是怎么都修改不出可以自动开关,都是只能到那个界面。
代码如下:
tell application “System Settings”
activate
— 跳转到功能键设置页面
do shell script “open x-apple.systempreferences:com.apple.Keyboard-Settings.extension?FunctionKeys”
end tell
delay 1 — 增加一点延迟,确保开关已经加载出来
tell application “System Events”
tell process “System Settings”
set frontmost to true
try
— 核心逻辑:在这个子窗口的所有层级里寻找第一个“开关 (group)”或“复选框”
— 在新版 macOS 中,这种开关往往被识别为可以点击的 group 或 switch
— 尝试路径 A:寻找第一个开关
set theToggle to first checkbox of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
click theToggle
delay 0.3
— 点击右下角的“完成”按钮
click button “完成” of window 1
on error
— 尝试路径 B:如果 A 失败,尝试通用的 UI 元素点击
try
— 直接通过 UI 元素索引点击,通常这是页面上第一个也是唯一一个开关
click checkbox 1 of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
click button “完成” of window 1
on error
display notification “坐标或路径匹配失败,请检查辅助功能权限。” with title “切换失败”
end try
end try
end tell
end tell
Leave a Reply Cancel reply