1

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.

KIRINPUTRA
  • 11
  • 2
  • Much thanks, Lauri, so far so good! I appreciate it. – KIRINPUTRA 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. – KIRINPUTRA 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... – KIRINPUTRA 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). – KIRINPUTRA 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. – KIRINPUTRA Apr 28 '13 at 22:13
  • So... I did more research and decided to try Quicksilver, but it crashed every time I tried to instruct it to run one of my Services from Automator. Then I tried instructing it to run my Apple scripts from before, but all keystroke combos have proven unresponsive... – KIRINPUTRA Apr 28 '13 at 22:15
  • I would appreciate any insight into this... There are a number of solutions on the web, involving a lot of heavy lifting for newbies like myself. Hopefully we will find a solution to this problem and then I will post it to a blog or other site so that the next person won't have to go through this. Thanks to everyone in advance! – KIRINPUTRA Apr 28 '13 at 22:17
  • Problem completely solved. Much thanks to Lauri, Asmus et al. I will be rewriting a step-by-step guide to this soon. I'll post a link here. – KIRINPUTRA Apr 28 '13 at 23:45

1 Answers1

0

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.

Lri
  • 40,894
  • 7
  • 119
  • 157