首页 > 解决方案 > 在 macOS Catalina 中使用 Applescript 切换 fn 键

问题描述

我正在尝试创建一个自动化快捷方式来切换触控栏中的 fn 键。

到目前为止,这是我的方法:

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.keyboard"
    //TO-DO: script that opens drop down menu in settings and changes it to display fn buttons

end tell

标签: macosautomationapplescriptmacos-catalina

解决方案


对于其他试图完成这项工作的人 - 我终于得到了我的解决方案。测试:MacOS Big Sur,11.4,2021 年 6 月

代码基于这里: https ://github.com/MrSimonC/Toggle-Mac-Function-Keys

但为简洁起见,这里是苹果脚本文件的内容:

-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Big Sur (11.4) June 2021
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    if UI elements enabled then
        tell application process "System Preferences"
            repeat until exists tab group 1 of window "Keyboard"
                delay 0.5
            end repeat
            click radio button "Keyboard" of tab group 1 of window "Keyboard"
            click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
        end tell
        tell application "System Preferences" to quit
    else
        -- GUI scripting not enabled.  Display an alert
        tell application "System Preferences"
            activate
            set current pane to pane "com.apple.preference.security"
            display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
        end tell
    end if
end tell

希望有人觉得它有用!西蒙。


推荐阅读