Re: Swap keyboard from "belgium" layout to US
Re: Swap keyboard from "belgium" layout to US
- Subject: Re: Swap keyboard from "belgium" layout to US
- From: kai <email@hidden>
- Date: Fri, 21 Apr 2006 12:46:02 +0100
On 20 Apr 2006, at 12:45, Felix Grasser wrote:
Kai, you just made my day!
I have been trying to implement a targeted keyboard switcher for a
long time. Unfortunately, I only got as far as Gary's suggestion of
tell application "System Events" to keystroke space using
command down
Because I have more than just two input sources, the script would
just be stuck with the wrong language from time to time. Your
proposition finally allows a targeted selection of the "French" or
"German" keyboard layout -- wonderful.
I'm just delighted to hear that someone found it useful, Felix. :-)
Interestingly enough, your first solution
[snip: code]
Gets stuck on 'is "text input menu extra"' (Tiger 10.4.6.) with the
error message reading: "AppleScript Error: No result was returned
from some part of this expression."
When opening the UI inspector application, the "AXDescription" is
present and reads "text iniput menu extra".
Do you have any idea what might be happening?
Interesting. I haven't yet had a chance to upgrade from 10.4.5, so it
still works fine here. (Perhaps something got dented in 10.4.6.) Do
you get a decent result from this next snippet?
------------------
tell application "System Events" to tell menu bar 1 of ¬
application process "SystemUIServer" to value of ¬
attribute "AXDescription" of menu bar items
-- OMM:
--> {"AppleScript menu extra", "modem menu extra", "text input menu
extra", "battery menu extra", "clock menu extra"}
------------------
If that gives you a half-sensible answer, it might be worth trying
this slightly modified (and untested) version of the
'switch_keyboard' handler:
------------------
on switch_keyboard to keyboard_layout
tell application "System Events" to tell menu bar 1 ¬
of application process "SystemUIServer"
(* workaround for bug in conditional filter: *)
repeat with this_menu from 1 to count menu bar items
tell menu bar item this_menu to if ¬
(get value of attribute "AXDescription") ¬
is "text input menu extra" then
click (* open menu *)
tell menu item keyboard_layout of ¬
menu 1 to if exists then return click
cancel (* operation failed: close menu *)
exit repeat
end if
end repeat
end tell
beep (* indicate that a failure occurred *)
error number -128 (* cancel script execution *)
end switch_keyboard
switch_keyboard to "U.S."
delay 2 (* demo only: do some stuff *)
switch_keyboard to "Belgian"
------------------
Your second "try-and-find-the-menu-item-location" script works fine
and returns a menu position of 3. Pasting that into the third
"switch-input-menu-based-on-menu-position" script works perfectly
and switches back and forth at will.
Perfection!
But since you and others on this list are obviously so good at this
UI scripting thing, why not take advantage of that ;-)
I would like to combine the switching of the input menu with a
switch in spell checker language.
What I have managed so far is to bring up the spell checker panel
through a custom keyboard shortcut assigned to the cocoa action
"showGuessPanel:".
Btw, is there a way of calling a cocoa action directly from
AppleScript?
Not directly - 'call method' is defined only in applications that
link to AppleScriptKit (which an AppleScript Studio application does
implicitly). It's possible to make obj-c calls through an application
like Automator:
------------------
tell application "Automator"
set l to call method "knownTimeZoneNames" of class "NSTimeZone"
tell me to set z to choose from list l with prompt "Choose a time
zone:"
if z is false then error number -128
tell (call method "timeZoneWithName:" of class "NSTimeZone" with
parameters z) ¬
to set d to z's beginning & ":" & return & (current date) - (time
to GMT) ¬
+ (call method "secondsFromGMT" of it) & ¬
{"", " (DST)"}'s item (1 + (call method "isDaylightSavingTime" of it))
end tell
display dialog d
------------------
However, unless Automator already happens to be open, that could be a
bit of a sledgehammer to crack a nut (especially for just the
occasional, one-line sniff). Another way might be to make a
background-only Studio Application dedicated to offering similar
access - which is pretty much what Daniel Jalkut (of FastScripts/
Clarion fame) did. I believe his app can still be downloaded from:
http://www.red-sweater.com/AppleScriptKit.zip
However, once the once the spelling window is activated, it is
visible on screen, but I can't manage to actually access the
elements. I.e. the switch_current_lang procedure below does not work.
Does anyone have an idea as to what I am doing wrong and how one
could successfully switch to another language?
I'm not entirely sure if our spelling windows differ in any
significant way, Felix - but I've cobbled together a possible
alternative for your 'switch_current_lang' handler (which is the
crucial part for this discussion). The modified version (further
below) works fine, here - but when was that ever a guarantee? ;-)
P.S. Here is what I have so far:
[snip: code]
on switch_current_lang(app_name)
tell application "System Events"
tell process app_name
tell window name_of_spelling_window
tell group 1
tell pop up button 1
set current_lang to value as string
tell menu name_of_menu
try
click menu item lang_german
on error
display dialog "Could not select " & lang_german & " as a
dictionary." buttons {"OK"} default button 1
end try
end tell
end tell
end tell
click (the first button whose subrole is "AXCloseButton")
end tell
end tell
end tell
end switch_current_lang
[snip: more code]
OK - so try this, then. If it doesn't work for you, it might be worth
going off-list to hammer it out - so that we don't bother these good
folks with all the minutiae...
------------------
on switch_current_lang(app_name)
tell application "System Events" to tell window ¬
name_of_spelling_window of process app_name
tell pop up button 1 of group 1
repeat until exists
delay 0.1
end repeat
click
tell menu item lang_german of menu 1 to if exists then
click
else
tell application app_name to display dialog "Could not select " & ¬
lang_german & " as a dictionary." buttons {"OK"} default button 1
end if
end tell
click button 1
end tell
end switch_current_lang
------------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden