Search.

Google
 

Tuesday, July 8, 2008

HOWTO: Quickly toggle function keys on and off on OS X

Let me say that I love using the Mac, it works great and runs really well. For the most part. I like using the apple keyboard because I like all the special keys for controlling iTunes and what not. However, being that I don't have the 'aluminum' keyboard that has a function key, I've noticed that in order to use the special keys, I have to go in to Settings, go in to keyboard, and manually check or uncheck the option. This was driving me crazy, as quite often I need to use the terminal, and mc (midnight commander) via ssh to a linux machine - which needs the F keys to work as F keys.

Luckily, all is not lost! Thanks to applescript, we can toggle the option using script file. Open up 'Script Editor' and use this script:
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 & Mouse" of process "System Preferences"
click checkbox "Use all F1, F2, etc. keys as standard function keys"
end tell
else
tell application "System Preferences"
activate
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell


This means, when you run the script, it will open the system preferences menu (without making it active), set the option, and close it again. Now, in order to make this useful, we need a way to associate this with a hotkey. So, we need to get the program Spark. This will let you set any hotkey you want to any thing else you want. Once you download it and it opens, you'll add a new hotkey for AppleScript, pick the key (I use F19), and point it to the applescript file (or just use the code above).

Once it is saved, everything is set up and ready to go! Happy terminal-ing!

2 comments:

kalasz said...

Hey man, I googled for "as standard function keys" and found you post on the second page. It should be the 1st link cause it works! Thanks!

Reid said...

Thanks! Works great! This was the first time ever actually using the script editor (now called "Applescript editor") after having a Mac over three years!
As an update, to work on OSX 10.6, change this process

tell tab group 1 of window "Keyboard & Mouse" of process "System Preferences"

to this

tell tab group 1 of window "Keyboard" of process "System Preferences"

And it should work. All I did was edit "Keyboard & Mouse" by deleting "& Mouse" so it is "Keyboard"