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.

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
  • 42,502
  • 8
  • 126
  • 159