在macOS Catalina中使用Applescript切换fn键

时间:2019-12-03 14:34:56

标签: macos automation applescript macos-catalina

我正在尝试创建一个自动化快捷方式,以切换触摸栏中的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

2 个答案:

答案 0 :(得分:0)

这是我用于Catalina OS的代码:

tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.keyboard"
reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
end tell

tell application "System Events" to tell process "System Preferences"
    delay 0.3
    set thePopUp to pop up button 2 of tab group 1 of window "Keyboard"
    set current to value of thePopUp
    click thePopUp
    if current is equal to "F1, F2, etc. Keys" then
        click menu item 1 of menu 1 of thePopUp
    else
        click menu item 3 of menu 1 of thePopUp
    end if
end tell

quit application "System Preferences"

答案 1 :(得分:0)

对于任何其他尝试进行这项工作的人 - 我终于得到了我的解决方案。测试: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

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