How to create text substitution in command line instead of System Preferences > Keyboard > Text

Is there any way to create text substitutions in the command line instead of System Preferences > Keyboard > Text?

I see that when you export text substitutions, it creates an html file, so I'm not sure whether you need to create a similar html file to import them.

The reason for this is I have lots of shortcut keys that were created in Windows and would like to re-create them on a Mac. I would hate to have to create each of them in the GUI. I was wondering if there was a command line alternative as a Mac is a Unix flavored OS.


Solution 1:

Another option for generating the plist file to be imported would be a simple Python script. Python has built-in support for apple plist files.

Assuming a file (CSV) called "substitutions.csv" with the "shortcut" and "phrase" headers.

shortcut,phrase
mename,Frodo

And the Python script generate_substitions.py in the same folder as the .csv file above.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import plistlib

with open('./substitutions.plist', 'wb+') as _plist:
    with open('./substitutions.csv') as _f:
        csvfile = csv.DictReader(_f)
        plistlib.dump(list(csvfile), _plist)

Run this in in the terminal.

chmod +x generate_substitutions.py
./generate_substitutions.py

Then drag the generated .plist file into the System Preferences > Keyboard > "Text" text substitutions window. New substitions are added and existing ones (matching on the shortcut) are updated.