Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a follow-up question to this topic: Switch TO specific input source

Asmus posted this answer:

I came up with a bit nicer solution in AppleScript, given you know the name of the Keyboard Layout you want to switch to. Create a function like this:

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

and then call it by

 changeKeyboardLayout("English")
 changeKeyboardLayout("German")

Be aware that the names of Keyboard Layouts are localized, i.e. on a german system the above example would need to call "Englisch" and "Deutsch".

So, I tried creating a script like this, for switching to Vietnamese on a Spanish system:

on changeKeyboardLayout("Vietnamita")  
   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 "Vietnamita")}  
   end tell 
end changeKeyboardLayout

It's not working. It also doesn't work if I take out the quotation marks on either or both instances of the word "Vietnamita". Can you see what I'm doing wrong? Thanks.

share|improve this question
    
Much thanks, Lauri, so far so good! I appreciate it. –  AMHOANNA Apr 27 '13 at 21:51
    
Hi everyone... With Lauri's correction, I was able to create Apple scripts that toggle to specific keyboard layouts. –  AMHOANNA Apr 28 '13 at 22:05
    
Next, I went to Services to create shortcuts to the Apple scripts. The last section, Applications, allows us to select custom Applications to shortcut to. However, my Apple script files were greyed out and could not be selected... –  AMHOANNA Apr 28 '13 at 22:07
    
So even though I'm a newbie with very little programming experience, I pulled up different tutorials and decided to go the Automator route. Using Lauri's script, I created Services in Automator that did the same thing as my Apple scripts. Then I went to the Services section of the shortcuts list. My newly written Services were there... However, almost none of the keystroke combinations that I keyed in worked at all; the one or two that did work, only worked in some programs (Safari and TextEdit but not Notes or Firefox). –  AMHOANNA Apr 28 '13 at 22:11
    
In any case I need four or more keystroke combinations that are easily reached from a standard finger positioning, and this wasn't possible. –  AMHOANNA Apr 28 '13 at 22:13

1 Answer 1

You are supposed to call it like this:

on changeKeyboardLayout(layoutName)
    --
end changeKeyboardLayout
changeKeyboardLayout("Vietnamita")

Or just remove the handler:

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "text input")
        click
        click menu item "Vietnamita" of menu 1
    end tell
end tell

If you get an error like "Access for assistive devices is disabled", enable access for assistive devices in the accessibility preference pane.

You can also use changeInput or KeyRemap4MacBook. See this question.

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.