0

I have a simple script that I've used for years to toggle the function key (fn) off and on. The checkbox is located at System Preferences > Keyboard > Keyboard Tab > 3rd checkbox down. Unfortunately, with OS11, it stopped working. I've now tweaked the script, but it generates an error message every other time I run it.

I'm a novice, so there may be a more elegant way to do this than the script below--it's simply what I've been using.

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell process "System Preferences"
        click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window 1
    end tell
end tell
quit application "System Preferences"

This is the Invalid Index error I get:

Can't get <> 1 or window 1 of <> "System Preferences" of application "System Events". Invalid Index.

Weirdly, the script works when I remove the <quit application "system preferences"> line and run the script from Script Editor with System Preferences open (so I can watch what happens). When I close System Preferences and add back in the last line, <quit application "system preferences">, here's the error I get within Script Editor:

error "System Events got an error: Can’t get tab group 1 of window 1 of process "System Preferences". Invalid index." number -1719 from tab group 1 of window 1 of process "System Preferences"

Any help or advice is greatly appreciated!

-1

Depending on your mac, the name of the checkbox might be different (it did not work for me on my Macbook Air). I used index in the suggested solution instead so it should work on all Macs until Apple moves the checkbox in the panel.

Also for your information, the solution stopped working because of a racing condition. You could add added a delay in your initial solution and it would have worked.

Here is a script with index :

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell process "System Preferences"
        repeat until checkbox 3 of tab group 1 of window 1 exists
            delay 0.1
        end repeat
        click checkbox 3 of tab group 1 of window 1
    end tell
end tell
quit application "System Preferences"
0

This AppleScript code works for me on Big Sur.

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell process "System Preferences"
        repeat until checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard" exists
            delay 0.1
        end repeat
        click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
    end tell
end tell
quit application "System Preferences"

If that version throws errors… here is an alternate version that also works for me.

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
    repeat until checkbox 3 of tab group 1 of window "Keyboard" exists
        delay 0.1
    end repeat
    click checkbox 3 of tab group 1 of window "Keyboard"
end tell
quit application "System Preferences"
4
  • Oh—I see the issue, the checkmark is attempting to drop in before the window is fully loaded. When I try this script, however, it seems to hang the script (without checking the box) and I have to force quit it. Any advice? – Nandayo Dec 12 '20 at 20:41
  • I changed "Use F1, F2, etc. keys as standard function keys on external keyboards" to the number 3 in both places so it reads: 'checkbox 3 of tab group 1 of window "Keyboard"'. For some reason, this seems to be working. I'll report back once I've played with it some more. – Nandayo Dec 12 '20 at 21:06
  • @Nandayo If you found my answer to be the solution or to have helped you finding the correct solution, I would appreciate you up-voting and or accepting my post. – wch1zpink Dec 14 '20 at 16:56
  • Yes it helped, thank you! – Nandayo Dec 15 '20 at 18:06
0

I had similar issues with my expand/compress screen resolution script and I fixed it by refactoring to use tell directly on the tab groups. Maybe you can try:

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell process "System Preferences"
        tell tab group 1 of window "Keyboard"
            click checkbox "Use F1, F2, etc. keys as standard function keys"
        end tell
    end tell
end tell
quit application "System Preferences"

For reference, my case was this script that worked pre-Big Sur

on run {input, parameters}
    
    activate application "System Preferences"
    tell application "System Events"
        tell process "System Preferences"
            click button "Displays" of scroll area 1 of window "System Preferences"
            delay 0.5
            
            click radio button "Scaled" of radio group 1 of tab group 1 of window "LG ULTRAWIDE"
            delay 0.5
            select row 4 of table 1 of scroll area 1 of tab group 1 of window "LG ULTRAWIDE"
            
        end tell
        delay 1
        quit application "System Preferences"
    end tell
    
    return input
end run

and the changes I had to make for it to work after Big Sur

on run {input, parameters}
    
    activate application "System Preferences"
    tell application "System Events"
        tell process "System Preferences"
            click button "Displays" of scroll area 1 of window "System Preferences"
            delay 0.5
            
            tell tab group 1 of window "LG ULTRAWIDE"
                click radio button "Scaled"
                delay 0.5
                select row 4 of table 1 of scroll area 1
            end tell
        end tell
        delay 1
        quit application "System Preferences"
    end tell
    
    return input
end run
1
  • Why and how does this work – mmmmmm Jan 25 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .