Script to change System Preference function key settings no longer working in Big Sur OS11
-
31-05-2021 - |
Domanda
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:
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!
Soluzione
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"
Altri suggerimenti
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"
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