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: Felix Grasser <email@hidden>
- Date: Thu, 20 Apr 2006 13:45:55 +0200
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.
Interestingly enough, your first solution
------------------
on switch_keyboard to keyboard_layout
tell application "System Events" to tell menu bar 1 of application
process "SystemUIServer"
repeat with this_menu in menu bar items (* workaround for bug in
conditional filter *)
tell this_menu to if 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"
------------------
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?
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?
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?
Many thanks in advance
Felix
P.S. Here is what I have so far:
===========8><====================
-- Adjust for input menu.
-- Stings in quotation marks: name of the corresponding spelling
-- dictionary as given in the spelling prefs.
property lang_english : "English" --change to option given in menu
property lang_french : "Français" --change to option given in menu
property lang_german : "Deutsch" --change to option given in menu
-- Give identifiers for dictionaries (check file ".GlobalPreferences" in
-- Library/Preferences for available identifiers). The identifiers
-- specified hereafter concern the cocoa aspell dictionaries.
property lang_english_id : "en_GB"
property lang_french_id : "FrancaisSuisse"
property lang_german_id : "de_CH"
-- Adjust based on the names used in your computer's locale (i.e. system
-- language).
property name_of_spelling_window : "Spelling" --change to be as given
in your language
property name_of_menu : "OtherViews" --change to be as given in your
language
property error_message : "Spell options not available!"
--error message displayed when an application has no spelling option
for the current window
-- get name of current application
set app_name to my get_front_app()
-- try to activate spelling window in current app
activate_spelling(app_name)
delay 0.5
-- try to change spelling dictionary in current app (if spelling
window exists)
-- if it does not, change spelling dictionary default through terminal
-- so that on the next app launch, the correct spell checker is selected
try
switch_current_lang(app_name)
on error
switch_default_lang()
end try
--
------------------------------------------------------------------------
------
-- Below are the definitions of the different handlers called in the
above script
--
------------------------------------------------------------------------
------
on switch_default_lang()
set curr_default_lang to do shell script "defaults read \"Apple
Global Domain\" NSPreferredSpellServerLanguage"
-- Set the new default language to german.
do shell script "defaults write \"Apple Global Domain\"
NSPreferredSpellServerLanguage " & lang_german_id
end switch_default_lang
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
on activate_spelling(app_name)
tell application "System Events"
tell process app_name
keystroke "j" using control down
end tell
end tell
end activate_spelling
on get_front_app()
tell application "System Events"
keystroke "h" using command down
delay 0.5 --need to adjust for your machine
set appname to name of (first process whose frontmost is true) as
string
--set appname to name of (info for (path to frontmost application))
end tell
return appname
end get_front_app
===========><8==================== _______________________________________________
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