OK, building off of a post on SuperUser, here goes:
You can create AppleScripts to change to the various languages. If you make Services that take no input and just call this one script, they'll all live happily in the Services menu when you want them. Otherwise, use your AppleScript trigger method of choice.
To switch to, say, Greek, and bring up the keyboard viewer when you do, run this script:
tell application "System Events"
if exists process "Keyboard Viewer" then
display alert "running"
try
tell application "KeyboardViewer" to quit
end try
end if
end tell
tell application "Finder"
open item "System:Library:Input Methods:KeyboardViewer.app" of the startup disk
end tell
changeKeyboardLayout("Greek")
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
To switch back to the U.S. layout, killing the viewer when you do, use this:
tell application "System Events"
if exists process "Keyboard Viewer" then
display alert "running"
try
tell application "KeyboardViewer" to quit
end try
end if
end tell
changeKeyboardLayout("U.S.")
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
Substitute the names of whatever keyboard layouts you want in the changeKeyboardLayout("layout name")
command.
open -a
and launch
seem to keep it open as well. But if you show the keyboard viewer from the input menu and close the window, the process is quit immediately. So one workaround might be to emulate clicking the menu item:
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 where description is "text input") of menu bar 1
click
click menu item "Show Keyboard Viewer" of menu 1
end tell
end tell
Also, I can re-open the application after closing it via a keyboard shortcut ONLY WHEN I open the viewer via the menu button or settings.
If all windows have been closed, activate
doesn't usually open a new default window. reopen
would do that, but it doesn't seem to work with KeyboardViewer.
Best Answer
Launch Keyboard Viewer with a Service (Improved)
You can launch the Keyboard Viewer with a shortcut by using Automator and the OS X Services functionality.
The Keyboard Viewer program lives at
/System/Library/Input Methods/KeyboardViewer.app
(in versions prior to Lion, it may be at/System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/KeyboardViewerServer.app
). You open it with a hotkey by using Automator to create a simple launcher service.Add the Run AppleScript action to your workflow, and replace the text with the following lines:
A couple notes:
repeat
loop checks every 5 seconds if Keyboard Viewer has any open windows, and if not, quits the process.KeyboardViewer
withKeyboardViewerServer
. I don't have anything pre-Lion handy to test this (if someone else could report back in the comments, that would be great.