Applescript for changing the keyboard layout?

I tried kiwilegal’s script.
I got this report:

tell application “System Events”
click menu bar item 7 of menu bar 1 of application process “SystemUIServer”
“Erreur dans System Events : NSReceiverEvaluationScriptError: 4”

So I’m asking if as is the script is running for him.

Yvan KOENIG (from FRANCE mercredi 30 mai 2007 14:17:23)

Yvan

I have developed a script to switch languages in Entourage and Spell Catcher. I invoke it using Quicksilver as required (separate script for each preferred language). Script is as follows (may need adjustments to the number of key code 125s used for any particular language - I have not worked out how to directly select a language from the sub menu of the Spell Catcher menu bar drop down menu). I also have a version for Word 2004. If you would like that, just post a reply.

Bien à vous


try
	-- Routine to switch language in Entourage
	tell application "System Events"
		tell application process "Microsoft Entourage"
			click menu item "English (UK)" of menu 1 of menu item "Change Dictionary" of menu 1 of menu bar item "Tools" of menu bar 1
		end tell
	end tell
	-- Following two lines needed to switch in and out of Entourage before changinf language in Spell Catcher
	tell application "System Events" to activate
	tell application "Microsoft Entourage" to activate
	
	
	-- Routine to switch language in Spell Catcher
	
	set mnu to "text input menu extra"
	set mnuItem to "Set Language"
	
	tell application "System Events" to tell process "SystemUIServer"
		set trk to get value of attribute "AXDescription" of every menu bar item of menu bar 1
		set {k, j} to {count of trk, 0}
		repeat with i from 1 to k
			if trk's item i is mnu as text then
				set j to i
				exit repeat
			end if
		end repeat
	end tell
	
	if j > 0 then
		try
			tell application "System Events"
				tell application process "SystemUIServer"
					tell menu bar item j of menu bar 1
						click
						delay 2
						tell menu 1
							click menu item mnuItem
							delay 0.2
							-- key down to set British English
							key code 125
							delay 0.5
							key code 125
							delay 0.5
							key code 36
						end tell
					end tell
				end tell
			end tell
		on error
			tell application "System Events" to key code 36
			display alert mnuItem & " menu item not found. Try again" giving up after 2
		end try
	end if
	
	display alert "Entourage/Spell Catcher language is now English...." giving up after 2
	
on error
	display alert "Operation failed" giving up after 2
end try

Thanks

I doesn’t use the named app: Entourage, Spell Catcher and QuickSilver so, I will simply save the passed script in my archives.

Yvan KOENIG

Hello. I’m trying to find an Apple Script to set me input method to Devanagari-QWERTY and then back to U.S. Extended. (Actually two scripts!)

Anyways a nice person over on the apple forums linked me to this thread and it seems VERY promising. But I’m having a small problem with Yvan KOENIG’s scripts. They all give me the following error:

System Events got an error: NSReceiverEvaluationScriptError: 4

And then highlight the word “value.” Any idea what could be causing that and how I deal with it? I am a complete novice at AppleScript so I don’t even know how to begin trying to figure this out. Thanks for any help/hints you can provide.

Hello

May you check that UI element scripting is enabled. which means that access for assistive devices is enabled in the Universal Access System Preference ?

Yvan KOENIG

That would be it! Sweet that seems to work great. Thank you very much.

I’ve been following this great thread and have finally come to a solution that works on my setup:
12 menu bar items including displays and third party menu extras
keyboard layouts:
Dvorak
Dvorak - QWERTY ‘clover leaf’
U.S.
U.S. Extended
Swiss German
(not tested but will also have to work on various flavours of Dvorak and Swiss layouts, for the final implementation should work)

key codes and keystrokes are not an option on my system - don’t ask, I’ve given up on that issue

the rough script:


set theMenuName to "text input menu extra"
set theLabelToClick to 1 --""

tell application "System Events" to tell application process "SystemUIServer"
	set menuBar to menu bar 1
	--> causes trouble: menu bar items of menuBar whose value of attribute "AXDescription" is theMenuName
	--set trk to get value of attribute "AXDescription" of every menu bar item of menu bar 1
	--set trk to value of attribute "AXDescription" of every menu bar item of menu bar 1
	set menuBarItemsList to value of attribute "AXDescription" of menu bar items of menuBar
	log "get the nuumber for: text input menu extra"
	set theMenuNum to my positionOfAinListB(theMenuName, value of attribute "AXDescription" of menuBar's menu bar items)
	if -1 ≠ theMenuNum then
		set theMenu to menuBar's menu bar item theMenuNum
		tell theMenu --the icon on menu bar
			repeat 3 times -- just handy for debugging
				log "the loop"
				-- can only get menu items if menu dropped
				if not selected then click
				set namesOfMenuItems to name of menu items of menu 1
				-- which layout do we have now? NOTE: the clover leaf doesn't seem to bother here!
				if "Dvorak - Qwerty " = value then
					if "Swiss German" is in namesOfMenuItems then
						-- click and make sure the value changed!
						my clickThisMenuItem(theMenu, "Swiss G", first menu item of menu 1 whose name is "Swiss German")
						-- NOTE: in the above line the filter works on "Swiss German" but not on clover leaf
					else
						beep
						log "Didn't  find Swiss German"
					end if
				else if "Swiss German" = value then
					-- this loop is ugly way to find out which item's name begins with to avoid having to deal with symbols and keyboard shortcuts
					-- can't use the positionOfAinListB routine because of the name's last character 
					-- if "Dvorak -" isn't found, targetMenuItem is the last item -> in my case: Open International
					repeat with i from 1 to count (menu items of menu 1)
						set targetMenuItem to menu item i of menu 1
						if targetMenuItem's name begins with "Dvorak -" then exit repeat
					end repeat
					-- click and make sure the value changed!
					my clickThisMenuItem(theMenu, "Dvorak -", targetMenuItem)
				end if -- SG current
				log "loop"
			end repeat -- debugging
		end tell -- theMenu
	else
		log "couldn't find menu bar item"
	end if -- theMenuNum
end tell

to clickThisMenuItem(theMenu, menuItemNameStart, Menuitem)
	-- using timeout  might be a good thing... to avoid an infinite loop
	tell application "System Events" to tell application process "SystemUIServer" to tell theMenu
		repeat until value begins with menuItemNameStart
			-- can't click empty space
			if not selected then click
			-- finally click the correct layout
			tell Menuitem to click
			delay 0.2
		end repeat
	end tell -- theMenu
	--end
end clickThisMenuItem

to positionOfAinListB(itemA, listB)
	-- this doesn't work on all layouts -> clover leaf
	if (itemA is in listB) then
		-- going backwards as I have my kyb layout menu extra far to the right, at the end of listB
		repeat with i from (count listB) to 1 by -1
			if itemA = listB's item i then return i
		end repeat
	else
		return -1
	end if
end positionOfAinListB

Thanks to all participants :lol:

Resurrecting this thread since now things seem to have changed with Leopard, and all the previous messages seem broken (on my system at least). So is there an easy way to select a keyboard with Applescript or Automator?

[edit: I’ve just spotted that changing
“text input menu extra” to “text input” tends to fix things!
]

Many thanks,
Macgruder

Hi,

the name of the text input menu in Leopard is “text input”
maybe it’s sufficient to change the value of the first line

Hi Stefan,
Yes, I just spotted that and it seems to work well. Many thanks for your help,
Macg