Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.50 average. Display Modes
Old 09-01-2005, 07:41 AM   #1
hegazy
Prospect
 
Join Date: Apr 2005
Posts: 2
Change keyboard Applescript

Hi there,
Anyone knows how to change the keyboard layout using Applescript or Realbasic, On OS X?

Any help will be apreciated.
hegazy is offline   Reply With Quote
Old 06-15-2007, 04:59 AM   #2
SwissalpS
Prospect
 
Join Date: Mar 2006
Posts: 3
Jaguar Using AppleScript UI Scripting

Quote:
Originally Posted by hegazy
Hi there,
Anyone knows how to change the keyboard layout using Applescript on OS X?

I need to switch between "Dvorak - Qwerty" and "Swiss German" a lot.
Key codes and keystrokes don't work on my setup - my code solves that too
Some times the layout doesn't change after the first click (scripted and by mouse) so this code only carries on after the layout has been changed ->
! could result in an infinite loop !
Code:
set theMenuName to "text input menu extra"

tell application "System Events"
	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 "|tBC"
		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
					-- can only get menu items if menu dropped
					if not selected then click
					set namesOfMenuItems to name of menu items of menu 1
					if "Dvorak - Qwerty " = value then
						if "Swiss German" is in b then
							my clickThisMenuItem(theMenu, "Swiss G", first menu item of menu 1 whose name is "Swiss German")
						else
							beep
							log "Didn't  find Swiss"
						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 
						repeat with i from 1 to count (menu items of menu 1)
							set c to menu item i of menu 1
							if c's name begins with "Dvorak -" then exit repeat
						end repeat
						my clickThisMenuItem(theMenu, "Dvorak -", c)
					end if
					log "loop"
				end repeat
			end tell
		end if
	end tell
end tell

to clickThisMenuItem(theMenu, menuItemNameStart, Menuitem)
	-- using timeout 
	tell application "System Events"
		tell application process "SystemUIServer"
			tell theMenu
				repeat until value begins with menuItemNameStart
					if not selected then click
					tell Menuitem to click
					delay 0.2
				end repeat
			end tell
		end tell
	end tell
	--end
end clickThisMenuItem

to positionOfAinListB(itemA, listB)
	if (itemA is in listB) then
		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
It works for me for the time being, as rough as it is.
SwissalpS is offline   Reply With Quote
Old 06-15-2007, 08:10 AM   #3
biovizier
All Star
 
Join Date: May 2004
Location: london on ca
Posts: 930
Quote:
I need to switch between "Dvorak - Qwerty" and "Swiss German" a lot.

If you are only switching between two, you could also enable the "System Preferences" > "Keyboard & Mouse" > "Input Menu" > "Select the previous input source" option. For years (at least as far back as the System 7 era), the keyboard shortcut was ?Spacebar although that was unceremoniously be co-opted by Spotlight in 10.4, resulting in a conflict. At least it remains customizable...
biovizier is offline   Reply With Quote
Old 06-15-2007, 02:48 PM   #4
SwissalpS
Prospect
 
Join Date: Mar 2006
Posts: 3
Quote:
Originally Posted by biovizier
If you are only switching between two, you could also enable the "System Preferences" > "Keyboard & Mouse" > "Input Menu" > "Select the previous input source" option. For years (at least as far back as the System 7 era), the keyboard shortcut was ?Spacebar although that was unceremoniously be co-opted by Spotlight in 10.4, resulting in a conflict. At least it remains customizable...

I can't use keystrokes or key codes so that's not an option on my system.

On a system where AS can successfully initiate keystrokes, I would agree.
SwissalpS is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 04:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.