Hi,
I’m currently attempting to toggle the function keys in the system settings on macOS Ventura 13.3, i can’t get it to perform a button press.
Unfortunately i don’t have UI Browser to dig deeper.
AppleScript
tell application "System Settings"
activate
reveal pane id "com.apple.Keyboard-Settings.extension"
tell application "System Events"
tell process "System Settings"
-- tell group 2 of scroll area 1 of group 1 of group of splitter group 1 of group 1 of window "Keyboard"
tell button 1 of group 2 of scroll area 1 of group 1 of group of splitter group 1 of group 1 of window "Keyboard"
-- set uiElementslist to UI elements
perform action "AXPress"
end tell
end tell
end tell
end tell
Open Keyboard settings, the tab were the UI element “Use F1, F2, etc keys as standard standard function keys” is located.
Open in automator new Workflow document, click on RED button (it starts recording you manual actions).
Go back to Keyboard window, click on UI element “Use F1, F2, etc keys as standard standard function keys”.
Stop Record.
In Automator window you will find the recorded actions. Drop the last action to the Script area (the window under the recorded actions) to find in it (the recorded script) the correct reference to clicked UI element “Use F1, F2, etc keys as standard standard function keys”.
NOTE: using similar Automator way, you can find correct reference to any clicked UI Element, always. No need UI browsers.
I think i’m pretty close but i can’t get it to click the button.
AppleScript
tell application "System Settings"
activate
reveal pane id "com.apple.Keyboard-Settings.extension"
delay 1
tell application "System Events"
tell process "System Settings"
click UI element 12 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Keyboard"
delay 1
tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
select row 11
delay 0.3
tell UI element 1 of row 11
click button 1
end tell
end tell
end tell
end tell
end tell
Automator
on run {input, parameters}
-- Click the text “Keyboard”
delay 1.882038
set timeoutSeconds to 2.0
set uiScript to "click static text 1 of window \"Keyboard\" of application process \"System Settings\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” button.
delay 0.926165
set timeoutSeconds to 2.0
set uiScript to "click UI Element 12 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window \"Keyboard\" of application process \"System Settings\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” button.
delay 1.720122
set timeoutSeconds to 2.0
set uiScript to "click UI Element 1 of UI Element 1 of row 11 of outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of sheet 1 of window \"Keyboard\" of application process \"System Settings\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” checkbox.
delay 1.374169
set timeoutSeconds to 2.0
set uiScript to "click checkbox 1 of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window \"Keyboard\" of application process \"System Settings\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” button.
delay 1.292147
set timeoutSeconds to 2.0
set uiScript to "click UI Element 2 of group 2 of splitter group 1 of group 1 of sheet 1 of window \"Keyboard\" of application process \"System Settings\""
my doWithTimeout(uiScript, timeoutSeconds)
return input
end run
on doWithTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
try
run script "tell application \"System Events\"
" & uiScript & "
end tell"
exit repeat
on error errorMessage
if ((current date) > endDate) then
error "Can not " & uiScript
end if
end try
end repeat
end doWithTimeout
I looked into the script that Automator wrote you. Pay attention to written action when clicked the checkbox. This should be what you need. Now we only need to remove the escape characters (\):
click checkbox 1 of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window “Keyboard” of application process “System Settings”.
NOTE: I have Catalina, so I can’t write fully-working script for Ventura. The reference above is only 1 action: click on checkbox “Use F1, F2, etc keys as standard standard function keys”. Maybe, you need other clicks before and after this action, I don’t know to help.
I just wanted to show you that Automator can “spy” for you by writing in its code, among other things, the correct references you need, which you can copy and paste into your script.
Your script should be like this:
tell application "System Settings"
activate
reveal pane id "com.apple.Keyboard-Settings.extension"
end tell
tell application "System Events" to tell process "System Settings"
repeat until window "Keyboard" exists -- automated delay
delay 0.2
end
-- before clicks
click checkbox 1 of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
-- after clcks
end tell
It seems like you should use System Settings to get to the actual place where the function keys are toggled, which should reduce the amount of GUI scripting. I tested this on my Ventura 13.3.1 computer without issue.
tell application "System Settings"
activate
reveal anchor "FunctionKeys" of pane id "com.apple.Keyboard-Settings.extension"
delay 3 -- test different values or use repeat loop
end tell
tell application "System Events" to tell process "System Settings"
-- put GUI scripting here to click on function-key toggle
end tell
Just wondering how is this different from this AppleScript
tell application "System Settings"
activate
reveal pane id "com.apple.Keyboard-Settings.extension"
delay 1
tell application "System Events"
tell process "System Settings"
click UI element 12 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Keyboard"
delay 1
tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of sheet 1 of window "Keyboard"
select row 11
delay 0.3
tell UI element 1 of row 11
click button 1
end tell
end tell
end tell
end tell
end tell
@Jacck. I tested your script on my Ventura computer. It activated System Settings and selected “Keyboard”, after which it reported the following error:
System Events got an error: Can’t get UI element 12 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window “Keyboard” of process “System Settings”. Invalid index.
I retested my script from post 6 in Script Debugger. The script effectively 1) activated "System Settings; 2) selected “Keyboard”; 3) opened the “Keyboard Shortcuts” dialog; and 4) selected “Function Keys”. At this point, the right pane contains the option to toggle function keys as “standard function keys”, and you would use GUI scripting to change this preference.
Most forum member prefer scripting an app directly to the extent that’s possible, but, obviously, you can use GUI scripting instead if that’s your preference.