I’m using the below code to update the modifier keys to my requirement.
Is there a way to update this in the background without the user’s notice?
tell application "System Preferences"
if not running then
run
tell application "System Events"
set bailOut to 20
repeat until (exists of application process "System Preferences") = true
set bailOut to bailOut - 1
if bailOut ≤ 0 then error "Timeout on Run System Preferences!"
end repeat
end tell
end if
--activate
set current pane to pane "com.apple.preference.keyboard"
set bailOut to 20
repeat until name of front window = "Keyboard"
delay 0.1
set bailOut to bailOut - 1
if bailOut ≤ 0 then error "Timeout on Reveal Anchor Input of Keyboard Preference!"
end repeat
tell application "System Events"
tell process "System Preferences"
click button "Modifier Keys…" of tab group 1 of window "Keyboard"
click pop up button 6 of sheet 1 of window "Keyboard"
click menu item 2 of menu 1 of pop up button 6 of sheet 1 of window "Keyboard"
click pop up button 1 of sheet 1 of window "Keyboard" --Command
click menu item 8 of menu 1 of pop up button 1 of sheet 1 of window "Keyboard"
click button "OK" of sheet 1 of window "Keyboard"
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
I’ve been seeking to do something similar without bringing the preference window to the front. I’ve seen this in another forum. See if you are able to put something together.
I had to rearrange the script a bit to get things working reliably on my system, but this script is working consistently for me on Mojave.
Run does not bring system prefs to the fore on my system. If it does on yours that’s surprising, but perhaps launch will work instead.
-Chris
--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2023/01/28 03:12
# dMod: 2023/01/28 03:29
# Appl: System Events, System Preferences
# Task: Activate Button [Modifier Keys] of System Keyboard Preferences.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @System_Preferences, @Activate, @Button, @Modifier_Keys, @Keyboard, @Preferences
# Test: macOS 10.14.6 Mojave
--------------------------------------------------------
with timeout of 30 seconds
tell application "System Preferences"
if not running then
launch
tell application "System Events"
set bailOut to 20
repeat until (exists of application process "System Preferences") = true
set bailOut to bailOut - 1
if bailOut ≤ 0 then error "Timeout on Run System Preferences!"
delay 0.25
end repeat
end tell
end if
reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell application process "System Preferences"
set bailOut to 20
repeat until exists of window "Keyboard"
set bailOut to bailOut - 1
if bailOut ≤ 0 then error "Timeout on Reveal Anchor Input of Keyboard Preference!"
delay 0.25
end repeat
tell window "Keyboard"
tell tab group 1
tell button "Modifier Keys…"
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
end timeout
--------------------------------------------------------
Instead of doing the GUI way why not use “defaults read/write” command with a “do shell script”.
Since every setting is in a .plist preference file you can change the settings this way.
By the way, On my Macs there is no popup button 6. Only are 5.
Also each menu only has 7 items, not 8.
What menu & menu-item specifically are you trying to click (name please, not number)
so i can test properly?