Maybe-too-technical-filter: Change system prefs from command line?
September 5, 2008 9:48 AM   RSS feed for this thread Subscribe

I want a button to toggle my MacBook function keys (much technical info inside)

MacBooks have a row of dual function keys at the top of the keyboard (F1-12, doing double duty as louder/softer, brighter/dimmer, etc). You can change their "normal" behavior from one to the other via the System Preferences > Keyboard & Mouse > "Use all F1, F2, etc. keys as standard function keys" checkbox.

What I want is an icon I can put on my dock and click to toggle this preference. At first, you'd think that this shell script like this would do it:
#!/bin/bash                                                                     
curState=`defaults read "Apple Global Domain" com.apple.keyboard.fnState`
if [ $curState = "1" ]; then
    defaults write "Apple Global Domain" com.apple.keyboard.fnState	0
fi
{etc.}
... because it toggles the actual bit that is toggled by the checkbox in System Preferences. Unfortunately, it looks like the OS doesn't notice the change (when done this way) until you reboot. All the online discussion of this I could find typically ends at exactly this point.

So: I'm not picky about methods. If there's a way to force a re-load of the preferences from the command line, that's great. If there's a good Apple-scripty way to do it that's not crazy slow (e.g., bouncing script icon on dock, script load, bouncing system prefs icon, system prefs window open for 1ms, exit -- I already wrote that script), that'd be cool too. Does Quicksilver have a hook into this pref? That'd be great.
posted by range to computers & internet (8 comments total)
You probably want to use UI Scripting, which shouldn't have any trouble at all selecting that checkbox the way OS X "expects" it.
posted by bcwinters at 10:01 AM on September 5, 2008


Oh, sorry, didn't catch that that's probably what you used in the "crazy slow" method.
posted by bcwinters at 10:02 AM on September 5, 2008


Looks like this applescript will do what you want.
posted by tip120 at 10:06 AM on September 5, 2008


Yep, tip120's applescript works exactly like you wanted.
posted by bonobothegreat at 11:40 AM on September 5, 2008


You can configure Quicksilver to run an Applescript as a trigger. I used it to set up a keyboard shortcut to toggle my display sleep time.

Simply put the script in a directory that Quicksilver sees and you can easily assign it to a trigger.
posted by prozach1576 at 12:03 PM on September 5, 2008


Thanks everybody. The fn script from Scriptbuilders looks like it might do the trick, though it's got a runtime error for me at the moment -- I'll take a closer look tonight at the source. And either way, got a pointer to a script resource I didn't know about -- thanks!

bcwinters, that's right -- the "naive" applescript approach results in a 5-10 second round-trip every time you start the script, which I suppose shouldn't be irritating but is given that you can *almost* change the flag via the command line instantly.
posted by range at 2:14 PM on September 5, 2008


Hmm, the fn script seems to be working fine for me (OS 10.5)
posted by bonobothegreat at 3:18 PM on September 5, 2008


Crazy... I'm not quite sure what did it (long day, not keeping good track of things) but a little editing of the fn script got it to run. There's something I don't quite understand about the syntax used (it's just a little different from the script floating around several other message boards) that helps it run faster and less obtrusively. Works great -- thanks again everybody.
posted by range at 4:28 PM on September 5, 2008


« Older I need recs for a lightweight ...   |   Less obvious things to bring w... Newer »
This thread is closed to new comments.