Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have figured out a workable shell script using osascript (AppleScript) to automatically switch to US keyboard layout when MacVim activates (triggered by Keyboard Maestro), and Swedish layout when it deactivates:

osascript > /dev/null 2>&1 <<-RAW
on changeKeyboardLayout(layoutName)
    tell application "System Events" to tell process "SystemUIServer"
        tell (1st menu bar item of menu bar 1 whose description is "text input") to {click, click (menu 1's menu item layoutName)}
    end tell
end changeKeyboardLayout

changeKeyboardLayout("Svensk - Pro")
exit $?

However, AppleScript is truly slow and I also don't prefer to see the text input menu in the statusbar to flash open when switching back and forth.


Is there an alternative to my AppleScript approach to instantly switch layout?

…I envision something like a defaults command or altering any .plist file as potential candidates for the job, only I don't know any specifics.

Two things related to the answer(s) I'm seeking:

  1. I'm keen to have the change propagate instantly – faster than the sluggish AppleScript execution.

  2. I prefer not installing new software daemons to achieve this. (Suggestions are always welcome, though, but 3rd party apps will likely not qualify as the accepted answer.)

share|improve this question
up vote 2 down vote accepted

~/Library/Preferences/ByHost/*.plist has an AppleSelectedInputSources key, but modifying it directly doesn't seem to actually change the layout.

Most of the solutions posted at MacScripter just use UI scripting as well.

There's a small Objective-C utility called changeInput though that should change the layout almost instantly.

share|improve this answer
Wow. That command really does the trick. Lots of thanks, Lri. – Henrik Feb 8 '12 at 12:18
The command's website is down, and now redirects to ads. – Spotlight Mar 23 '15 at 12:34

I write a little program on objective-c cover it in an Alfred Workflow, and you can try it: And now compile binary for cli 64 bit: on enter wait one argument like "" With BTT working faster.

share|improve this answer

Your Answer


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.