Skip to content

Instantly share code, notes, and snippets.

@webel
Last active November 14, 2025 10:54
Show Gist options
  • Select an option

  • Save webel/464f9c58b22979fa37d4d12eb6d4faae to your computer and use it in GitHub Desktop.

Select an option

Save webel/464f9c58b22979fa37d4d12eb6d4faae to your computer and use it in GitHub Desktop.
# Mac programmatically set input sources and dock mods

Mac programmatically set input sources and dock mods

This is not the way to go! Which I figured out after digging around and jotting down my findings. Maybe this'll come in handy for someone else, though. The last link in references describes "the way"

How do I run it?

I'll risk insulting your intelligence first thing, and just mention that this is for OS X only. Open the terminal and do the below to run the script:

> . {PATH-TO-SCRIPT}/set_defaults.sh

What does it do?

  • Adds, by default, input sources (a.k.a keyboard layouts)

    • Swedish
    • US international

    by adding the layouts to the com.apple.HIToolbox.plist.

  • Makes the Dock "hidden" by default with magnify on scroll over

What about my needs?

If you want to change this to suit your needs, I suggest you set the input sources once manually through the System Preferences, check what the values for KeyboardLayout ID and KeyboardLayout Name are in com.apple.HIToolbox.plist, and substitute in the corresponding variables in the script. How to read the .plist is described below!

File before modification

To see what your file looks like, type defaults read com.apple.HIToolbox. If nothing is returned, or an error of such, it means you don't have a user-level file (yet). When running the script one is created, (thanks to defaults write that does so by default).

{
  AppleCurrentKeyboardLayoutInputSourceID = "com.apple.keylayout.Swedish-Pro";
  AppleEnabledInputSources = (
    {
      InputSourceKind = "Keyboard Layout";
      "KeyboardLayout ID" = 0;
      "KeyboardLayout Name" = "U.S.";
    }
  );
}

References

How to add input sources programmatically

  • Stackoverflow Q&A clarifying the syntax on how to add a default input source.

Xkbswitch-macosx

  • A console layout swticher for Max OSX. Interesting as it is the only place I can find with an indication to how to extract the KeyboardLayout ID if unknown beforehand. (See xkbswitch/main.m)

Beginning Mac OS X Programming

  • defaults write will create a plist file that does not exist

Best description on the alternative methods and why

  • Describes a way using the Text Input Services API, which seems far better than my way.

Tips

  • defaults looks for a preference list file in ~/Library/Preferences/. Meaning, you don't need to append a path to find the system generated one (if present), and don't forget that there's no need for the .plist extension.
#!/bin/bash
###
# author Evita Stenqvist
#
# Adds US International and Swedish keyboard to input sources
# Will create a com.apple.HIToolbox for the current host if one is not present
###
# Colours
red='\033[0;31m'
green='\033[0;32m'
reset='\033[0m'
# Kill Preferences, just incase it's running, supress potential warning
# about no running process
killall 'System Preferences' &>/dev/null
layouts=('USInternational-PC' 'Swedish - Pro')
ids=('1500' '7')
if [ ! ${#layouts[@]} -eq ${#ids[@]} ] ; then
echo -e "${red}Number of layout names does not equal number of ids! ${reset}"
# Like pressing CTRL+C
kill -INT $$
fi
# The keys we have to add for each layout
apple_keys=("AppleEnabledInputSources" "AppleInputSourceHistory" "AppleSelectedInputSources")
# Create the XML entries with defaults write
defaults -host "${USER}" write com.apple.HIToolbox AppleCurrentKeyboardLayoutInputSourceID com.apple.keylayout.Swedish-Pro
for ((i=0 ; i<${#layouts[@]}; i++)) ; do
for key in ${apple_keys[@]} ; do
defaults -host "${USER}" write com.apple.HIToolbox \
$key \
-array-add "<dict><key>InputSourceKind</key><string>Keyboard Layout</string>"\
"<key>KeyboardLayout ID</key><integer>${ids[i]}</integer>"\
"<key>KeyboardLayout Name</key><string>${layouts[i]}</string></dict>"
done
done
# Set defaults for the Dock
# Will autohide and magnify!
defaults -host "${USER}" write com.apple.dock autohide 1
defaults -host "${USER}" write com.apple.dock largesize 65
defaults -host "${USER}" write com.apple.dock magnification 1
echo -e "${green}Successfully set default values for input sources and the dock! ${reset}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment