Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I toggle the "Use all F1, F2 as standard keys" setting very often and would like to automate it via a script (or a keyboard shortcut). Is it possible? I've tried to Google the right arguments for defaults write ... but unsuccessfully.

fn settings

share|improve this question

5 Answers 5

up vote 5 down vote accepted
tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of tab group 1 of window 1
end tell
quit application "System Preferences"

You could also use defaults write -g com.apple.keyboard.fnState -bool, but I don't know any way to apply the changes without logging out and back in.

share|improve this answer
    
Exactly what I was looking for, thanks. –  jkbr Aug 12 '12 at 15:08
2  
I've packed this script as an app using Automator so that it can be quickly run from Spotlight: github.com/jkbr/macos-fn-toggle –  jkbr Feb 21 '13 at 19:06
    
@jkbr Your app on github is just the sort of thing I was searching for. Thanks! –  mdwhatcott Sep 14 '13 at 17:38
1  
You can also save a script as an application from AppleScript Editor. Just paste the script to AppleScript Editor, press ⌘S, and set the format to application. –  ؘؘؘؘ Sep 14 '13 at 17:51
1  
On Mavericks you get an error like: "AppleScript Editor is not allowed assistive access." So you have to go to "Security and Privacy" in the Privacy Tab, select Accesibility and enable your app to do that. You will have to provide admin password to open the lock. –  user69047 Jan 30 at 15:28

You can read the current state with the following. (1 for F-keys, 0 for media/brightness etc)

defaults read "Apple Global Domain" "com.apple.keyboard.fnState"

You can write it, however it won't change until you reboot. (I tried killing Dock and Finder instead, no luck)

defaults write "Apple Global Domain" "com.apple.keyboard.fnState" "1" ## F1 F2 etc
defaults write "Apple Global Domain" "com.apple.keyboard.fnState" "0" ## Brightness/Media

I suspect somebody with more knowledge on what reads this plist file could write up a script to kill and restart whatever reads this plist, which should make it acknowledge the change.

share|improve this answer
    
Thanks, that's the correct key. Changing it directly however doesn't seem to change the behaviour. –  jkbr Apr 20 at 8:01

I don't know about script but with some handy reverse-engineering and Googling, I made this since I didn't like the existing AppleScript GUI stuff that was out there:

https://github.com/nelsonjchen/fntoggle

Compiled binary (Toss into your PATH or if you don't know what that is then /usr/local/bin/): https://github.com/nelsonjchen/fntoggle/releases

Call it from anything (ControlPlane, Automator, sh, etc) by running fntoggle on or fntoggle off.

This script does not require GUI accessibility to be turned on. It also does not require logging out and back in.

share|improve this answer

Some AppleScript that might help: Applescript to toggle F1-F11 keys as function keys - MacRumors Forums

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

    
Welcome to Ask Different! You've posted this as an answer, which is only for things that directly answer the question asked. Answers on Ask Different need to be more than just a link. It's okay to include a link, but please summarize or excerpt it in the answer. The idea is to make the answer stand alone. Please take a look at the FAQs for more info. Thanks. –  Nathan Greenstein Aug 12 '12 at 15:48

Actually, you don`t have to run any app with spotlight or anything, the best way in my opinion is to:

  1. Create new Service in Automator
  2. Drag: "Run Apple script
  3. In the "Service recieves selected": choose no input
  4. paste this code from answer above:

    tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell
    tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of tab group 1 of window 1
    end tell
    quit application "System Preferences" 
    
    1. Save as "yourname"
    2. Open System Preferences -> Keyboard -> Shortcuts
    3. In the left menu choose: "Services"
    4. In the right, scroll all the way down to the General, and assign your service some unique hotkey, I choose cmd+shift+L

    5. Thats it! It`s not very fast or anything, but you dont have to run any app or go to the Preferences yourself. Hope it helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.