Skip to content

Instantly share code, notes, and snippets.

@alampros
Last active July 4, 2016 05:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alampros/2b81af6cfc564f6accd3 to your computer and use it in GitHub Desktop.
Save alampros/2b81af6cfc564f6accd3 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

Kymer 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