Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppleScript to toggle "Use all F1, F2, etc. keys as standard function keys" in System Preferences > Keyboard.
--Originally posted by "kiodane" at http://forums.macrumors.com/showthread.php?t=383969
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 tab group 1 of window "Keyboard" of process "System Preferences"
click checkbox "Use all F1, F2, etc. keys as standard function keys"
if (do shell script "defaults read -g com.apple.keyboard.fnState") = "1" then
set fnStateRead to "Function Keys - F1, F2, F3..."
set fnStateMessageRead to "F keys are now real function keys."
else
set fnStateRead to "Special Keys - Volume, iTunes..."
set fnStateMessageRead to "F keys are now special media keys."
end if
do shell script "/usr/local/bin/terminal-notifier -message \"" & fnStateMessageRead & "\" -title \"" & fnStateRead & "\" -activate com.orderedbytes.ControllerMate4"
end tell
tell application "System Preferences"
quit
end tell
else
tell application "System Preferences"
set current pane ¬
to pane "com.apple.preference.security"
display dialog ¬
"Accessibility control is not enabled. Under Privacy => Accessibility, check the box next to the name of this application."
end tell
end if
end tell
@alampros

This comment has been minimized.

Copy link
Owner Author

commented Sep 30, 2014

Updated to work on yosemite. Also added notifications via terminal-notifier (you might have to futz with the path to the terminal-notifier binary).

@Kymer

This comment has been minimized.

Copy link

commented Aug 23, 2015

Is there a reason you prefer to use terminal-notifier over display notification? And isn't it easier to check the value of the checkbox, rather than reading the defaults?

it could simplify and make the script more readable, by maybe doing something along the lines of:

if value of box is 1 then
   display notification "Disabling function keys"
else
   display notification "Enabling function keys"
end if

click box
-- where box is a variable pointing to the checkbox

I'd like to hear your thoughts on this, I'm very new to AppleScript :)

Edit:
I just found out by messing around a bit you can actually open the correct tab and select Accessibility by doing:

tell application "System Preferences"
   reveal anchor "privacy_accessibility" of pane "com.apple.preference.security"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.