Jan 31, 2012

Mac: adding a keyboard layout using AppleScript

Update: This no longer works in Mavericks because Apple changed the whole layout window. Last working OS version was Mountain Lion.

We have a special keyboard layout in the country, where I live (Latvia). It includes certain special letters: like ā, ē, ū and so on. Many people here set Latvian keyboard layout as a default. However I cannot do that because this layout has one major disadvantage for people like me.

Those special letters are typed by one of two ways: Alt+normal letter or '+normal letter. While the first way is goot, the second one is bad for programmers. I often use ' in the code, so having ' as a "dead key" (this is how it is called officially) is not suitable. But I still have to type those latvian chars sometimes. Until yesterday I kept English US layout as a default and added Latvian manually when I had to type latvian letters. This is a multiple move-and-click process. Yesterday I finally found a way to automate addition and removal of the layout.

Here is the AppleScript:

tell application "System Preferences"
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"
tell window 1 of process "System Preferences"
tell row 98 of table 1 of scroll area 1 of tab group 1
click checkbox 1
if value of checkbox 1 is 1 then
set layout to "Latvian"
set layout to "U.S."
end if

--display dialog layout buttons "ok" default button "ok"
tell application "System Events" to tell process "SystemUIServer"

set _ to get value of attribute "AXDescription" of every menu bar item of menu bar 1
set {k, j} to {count of _, 0}
repeat with i from 1 to k
if _'s item i is "text input" then
set j to i
exit repeat
end if
end repeat

if j > 0 then
tell menu bar item j of menu bar 1
delay 0.1
click menu item layout of menu 1
delay 0.1
end tell
end if
end tell

end tell
end tell
end tell

quit application "System Preferences"

Next I complied it into the application and added it to Dock. Clicking the application simulates all necessary clicks for me. The application works like a toggle: switches on or off the layout.

What do you need to do if you want to adapt it to another layout? You need to find the number of your layout. For that you open the corresponding System Preferences window and count rows starting from one, including separator rows and indented rows. Here is this window:

Latvian layout is number 98. Change that in the script (shown in bold). Also change the name of the layout to exactly match yours.

Note: this script requires that you have only flag in the menu bar. If you have input layout name, it will not work.

The script is tested on Mac OS X Lion and provided as is without any support and/or responsibilities. The idea is not mine, I took various pieces of code from the Internet and assembled into one, changing and fixing some lines to make it all work together. So I do not claim the ownership. The script is provided with the hope that it will be useful.