Using applescript to change the keyboard layout

How do I use applescript to change the keyboard layout (as if I had done it through System Preferences), or is there a shell command I can use in the script?

I’d look here first: http://bbs.applescript.net/viewtopic.php?id=15339

That will turn on all the keyboard layouts. After a few changes, you might end up with this:

tell application "System Preferences"
	activate
	set current pane to pane id "com.apple.Localization"
	reveal (first anchor of current pane whose name is "InputMenu")
end tell

tell application "System Events"
	launch
	tell window 1 of process "System Preferences"
		tell row 38 of table 1 of scroll area 1 of tab group 1
			--row 38 is Australian
			select
			click checkbox 1
		end tell
	end tell
end tell

--quit application "System Preferences"

If you want to turn on multiple layouts, then try something like this:

tell application "System Preferences"
	activate
	set current pane to pane id "com.apple.Localization"
	reveal (first anchor of current pane whose name is "InputMenu")
end tell

tell application "System Events"
	launch
	tell window 1 of process "System Preferences"
		repeat with rowNumber in {47, 48} -- "Canadian" and "Canadian French - CSA"
			tell row rowNumber of table 1 of scroll area 1 of tab group 1
				select
				click checkbox 1
			end tell
		end repeat
	end tell
end tell

--quit application "System Preferences"

these are enabling new layouts, but how to change between the active layouts?

former times, in os9, the simple:

set keyboard layout to "U.S."

worked…

anything like this for osx?