Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Quickly changing input methods via an applescript

Hello all. I'm relatively new to Mac ownership, but I'm jumping in trying to customize some things.
This started in the following thread:
http://discussions.apple.com/thread.jspa?threadID=1332310&tstart=0

What I'm trying to do:
With several input methods active, switching between them by the default MacOS allowed methods gets to be rather tedious. For example, Japanese, where one may easily find themselves switching between 3 input methods: US or Romaji, Hiragana, and Katakana. I much prefer the one-hotkey-per-input-method approach of some other operating systems. Plus, I'd really like to use speech and just tell my mac "switch to katakana" or "switch to hiragana." If it were just those two, then the default hotkeys would work for such a thing, or even configuring Kotoeri to use CAPS to switch between them would suffice. Anyway, enough about WHY I want to do this.

In the thread above, I was pointed to this thread on MacScripter: http://bbs.applescript.net/viewtopic.php?id=18620&p=1

With a minor modification for Leopard (changing "text input menu extra" to "text input"), the script works. But it is slow. I then spent a little time tweaking and seem to have gotten something more efficient:

tell application "System Events" to tell process "SystemUIServer"
set _ to get value of attribute "AXDescription" of every menu bar item of menu bar 1
set {k, j} to {count of _, 0}
repeat with i from 1 to k
if _'s item i is "text input" then
set j to i
exit repeat
end if
end repeat

if j > 0 then
tell menu bar item j of menu bar 1
click
tell menu 1
click menu item "Katakana"
end tell
end tell
end if -- j > 0
end tell -- to process and System Events

This is acceptably fast when running from within Script Editor, but when saved as an application takes anywhere from 3 to 5 seconds to switch input methods. I might as well mouse it or tab through multiple selections with the standard hotkeys at that rate.

Is this normal?

Does anyone know of a simpler way to accomplish this than UI scripting? Apparantly in OS9 it was as simple as set keyboard layout to "Katakana" or some such. Does anyone know where I might look to find the magical setting that can change it directly?

I'm sure the time issue has to do with (a) starting the interpreter and (b) all the parsing of the menu properties to find the right menu, then clicking it to get access. I'm sure it would go a lot faster if there was a way to go directly to the menu and not have to click it. Even better would just be some system property that could be directly set.

Thanks for any help you could give me!

iMac core2duo, Mac OS X (10.5.1)

Posted on Jan 9, 2008 10:15 PM

Reply
2 replies

Jan 11, 2008 12:59 AM in response to jedigrover

Hello

Two things.

• When switching input mode in Kotoeri input method, you can use keyboard shortcuts as follows.
(At least so under OS9. Kotoeri under OSX may vary, though I think it's unlikely. Also please note that these keyboard shortcuts are only valid while you're in Kotoeri input method. To switch to Hiragana mode in Kotoeri while you're in other input method (such as Korean input method) or in other script system (such as Roman) you'd need other tricks, if any.)
E.g.
Option + Shift + z = Hiragana mode
Option + Shfit + x = Katakana mode
Option + Shift + c = Alphanumeric (full width) mode
Option + Shift + s = Alphanumeric (half width) mode

cf.
Language Kit Manuals
http://docs.info.apple.com/article.html?artnum=50037

pp. 37-39
Japanese Input Method Guide (ca. 22 MB)
http://manuals.info.apple.com/AppleSupportArea/Manuals/software/JapaneseInputGuide.PDF



• To speed up your applet execution, you may try the following consturct, that is to wrap your code in a script object and invoke 'run script' osax to run it.
(I'm not sure but if you can save the script as a compiled script and call it via keyboard shortcut, then it may be faster than saving it as an applet (script application). Also, if you're saving the script as an applet, save it as an application bundle that is to be run natively in x86 Mac. Applet saved as normal application (not application bundle) is run via Rosetta in x86 Mac and will be slower than necessary.)

--SCRIPT
script o
tell application "System Events" to tell process "SystemUIServer"
set _ to get value of attribute "AXDescription" of every menu bar item of menu bar 1
set {k, j} to {count of _, 0}
repeat with i from 1 to k
if _'s item i is "text input" then
set j to i
exit repeat
end if
end repeat

if j > 0 then
tell menu bar item j of menu bar 1
click
tell menu 1
click menu item "Katakana"
end tell
end tell
end if -- j > 0
end tell -- to process and System Events
end script
run script o
--END OF SCRIPT



Hope this may be of some help,
H

Jan 12, 2008 3:29 PM in response to Hiroto

Thanks!
Option + Shift + z, Option + Shift + x, and Option + Shift + s all seem to work, but not Option Shiftc (but that is only because I haven't enabled full-width Romaji as an option in the International Settings Pane). I was looking for a place to change these hotkeys, but it doesn't appear that there is one. At least I should be able to change my Windows laptop to conform to this and have a consistent set of keys. And on Mac OS, I can configure to toggle between ひらがな and カタカナ using the Caps Lock key. For that matter, I might eventually setup kotoeri to use JIS layout and learn to touchtype with direct kana entry. It certainly would seem to be faster. At present though, my 日本語 is so limited that I am slow anyway.

Saving as a script is faster. I'm not sure what to do to make scripts run from the finder--it always opens them in script editor, so I was under the assumption I had to save as an applet. Anyway, I saved as a script and put it in my speakable items folder and now I can say "set input hiragana" and my mac does it without me even having to move a finger...I just have to wait about the same amount of time it would've taken me to use the hotkeys. So I don't know if it will work out to be an optimal solution, but at least it is possible.

ありがとうございます!

Quickly changing input methods via an applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.