MacOS – Mavericks: How to ADD Input Source via plists/defaults

defaultsinput-sourcekeyboardmacospreferences

I'm trying to automate my OS X setup but I'm stuck trying to programmatically add a new Input Source or keyboard layout.

I usually use two layouts: regular US and US International, so what I'm trying to do is just add the US International layout.

Here's what I noticed so far. When I manually add the layout, I see this entry being added to my AppleEnabledInputSources key on the com.apple.HIToolbox plist:

--- a/def0.txt
+++ b/def1.txt
@@ -17,6 +17,11 @@
                 {
             "Bundle ID" = "com.apple.PressAndHold";
             InputSourceKind = "Non Keyboard Input Method";
+        },
+                {
+            InputSourceKind = "Keyboard Layout";
+            "KeyboardLayout ID" = 15000;
+            "KeyboardLayout Name" = "USInternational-PC";
         }
     );
     AppleInputSourceHistory =     (

I got this output by doing defaults read com.apple.HIToolbox. I can confirm the same addition by directly opening ~/Library/Preferences/com.apple.HIToolbox.plist.

So I decided that probably all I had to do was just add the same entry and it would all work. So I reverted the manual addition (and confirmed the entry from the plist was gone) and now tried:

$ defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '{InputSourceKind="Keyboard Layout"; "KeyboardLayout Name"="USInternational-PC"; "KeyboardLayout ID"=15000;}'

I confirm again via defaults read diffs that it adds the same entry to the plist, but it doesn't work!

I tried killing cfprefsd and SystemUIServer and even rebooting but none of these seem to work.

Does anyone have a clue about what might be happening here?

Best Answer

Turns out that the format I used for the AppleEnabledInputSources entry was bad because it considers everything as type string, but KeyboardLayout ID must be of type integer!

So the correct way of doing this would be via XML:

defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>15000</integer><key>KeyboardLayout Name</key><string>USInternational-PC</string></dict>'

After running that command and doing a logout/login, looks like things work fine.