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 typestring
, butKeyboardLayout ID
must be of typeinteger
!So the correct way of doing this would be via XML:
After running that command and doing a logout/login, looks like things work fine.