Add input source to keyboard settings via script

donifar
New Contributor II

Hi all,

Does anyone know how to add a second input source to the keyboard? I have the keyboard ID, but don't know how to add it to the input sources?

I would also like to show the input menu in menu bar for users.

Thanks in advance for any guidance.

7 REPLIES 7

chrisdaggett
Contributor II

@donifar Also have a need for this but have yet to find any solution. Surprised this isn't more common.

dsavageED
Contributor II

We used to reset these settings to our defaults on lab Macs in the past. I'd suggest you want to do this via a custom configuration profile as these settings are user level. Scripting them will be far more complex than deploying a config profile.

To add the Menu bar option the preference is com.apple.systemuiserver.plist the necessary plist content is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>menuExtras</key>
        <array>
            <string>/System/Library/CoreServices/Menu Extras/TextInput.menu</string>
        </array>
        <key>NSStatusItem Visible com.apple.menuextra.textinput</key>
        <true/>
    </dict>
</plist>

The preference holding the keyboard info is com.apple.HIToolbox.plist, add your required keyboards in the System Preferences then, again, use this to create a custom config profile. For example, British and Australian, with British as default would be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>AppleCurrentKeyboardLayoutInputSourceID</key>
        <string>com.apple.keylayout.British</string>
        <key>AppleEnabledInputSources</key>
        <array>
            <dict>
                <key>InputSourceKind</key>
                <string>Keyboard Layout</string>
                <key>KeyboardLayout ID</key>
                <integer>2</integer>
                <key>KeyboardLayout Name</key>
                <string>British</string>
            </dict>
            <dict>
                <key>InputSourceKind</key>
                <string>Keyboard Layout</string>
                <key>KeyboardLayout ID</key>
                <integer>15</integer>
                <key>KeyboardLayout Name</key>
                <string>Australian</string>
            </dict>
        </array>
        <key>AppleSelectedInputSources</key>
        <array>
            <dict>
                <key>InputSourceKind</key>
                <string>Keyboard Layout</string>
                <key>KeyboardLayout ID</key>
                <integer>2</integer>
                <key>KeyboardLayout Name</key>
                <string>British</string>
            </dict>
        </array>
    </dict>
</plist>

talkingmoose
Honored Contributor II

Recently had someone point me to this question. It's possible to use the defaults command to add an input source. I have an example on GitHub.

Here's a short script:

#!/bin/zsh

# adds the additional input source to the list of input sources
/usr/bin/defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>89</integer><key>KeyboardLayout Name</key><string>Latin American</string></dict>'

# enables the input menu in the menu bar
/usr/bin/defaults write com.apple.TextInputMenu visible -bool true

exit 0

luic6
New Contributor

Any one can tell me how to find out the keyboard ID

Harrie
New Contributor III

@luic6 try this. defaults read /Library/Preferences/com.apple.HIToolbox.plist AppleEnabledInputSources

Harrie
New Contributor III

@talkingmoose I used your script, thnx for that. Now when I open my system prefs I have 2 how do I make the just added one default? Or how can I remove one?

thnx

user-yXXbTiAqAO
New Contributor

@talkingmoose jamf should give the smartest way to do it...