Is There an Automated Way to Import Apple Keyboard Text Replacement Shortcuts Into KM?

I have a few hundreds ( ok I didn't count, but I have a lot ) of shortcuts stored in Apple's Keyboard Replacement setting ( System Preferences->Keyboard->Text screen ) that I would like to import into Keyboard Maestro.

Is there any automated way to do this? I created macro group for them, but don't know an automated way to import them.

Hey @majorgear,

No. Keyboard Maestro has no built-in importer for Text Substitutions.

But – can it be done? Sure.

  • Open the Keyboard Preferences and switch to the Text panel.
  • Select all of your Text Substitutions.
  • Drag them out onto the Desktop.
  • This produces a ‘Text Substitutions.plist’ file.

Run this macro to extract all of the Substitution/Shortcut pairs. Copy from the pop-up window and paste into TextEdit, BBEdit, or your favorite text editor.

Extract Text Replacement-Shortcut Pairs from a Text Substitutions Plist on the Desktop 1.00.kmmacros (6.0 KB)

Here's how to use that information to create new Text Expansion macros in Keyboard Maestro:

-Chris

4 Likes

Just a note here because I've known many people who did not know this... OS X/macOS text replacements sync to iOS and iPad OS. With hundreds, I'm guessing there are plenty worth moving over to Keyboard Maestro, but there may be some that are helpful to have in the Apple ecosystem (e.g., I definitely want the text replacement setups I have for my email addresses through Apple because I find typing email addresses annoying on the iPhone).

3 Likes

I have met this problem recently, so I add a side feather to a tool I have built previously.
This tool has perfectly solved this problem.

Here is its github link: https://github.com/KyleBing/wubi-dict-editor

How it works, import a formatted text file with code and word on it, then the tool will process this file. all you need to do is select words you want to export to plist and press the export button, a list file with words in it will automatically generate on your desktop.

content before is

工	a
戈	a
式	aa
戒	aa
工	aaa
工	aaaa

after 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">
  <array>
    <dict>
      <key>phrase</key>
      <string>工</string>
      <key>shortcut</key>
      <string>a</string>
    </dict>
    <dict>
      <key>phrase</key>
      <string>戈</string>
      <key>shortcut</key>
      <string>a</string>
    </dict>
    <dict>
      <key>phrase</key>
      <string>式</string>
      <key>shortcut</key>
      <string>aa</string>
    </dict>
    <dict>
      <key>phrase</key>
      <string>戒</string>
      <key>shortcut</key>
      <string>aa</string>
    </dict>
    <dict>
      <key>phrase</key>
      <string>工</string>
      <key>shortcut</key>
      <string>aaa</string>
    </dict>
  </array>
</plist>

@ccstone - Brilliant solution! :smiley: :smiley:

My use case is to easily search 60 expense accounts to grab the GL account ID. Got this done quickly using this macro!

:exclamation: Note that the "&" character crashes the macro, so I just replaced any of those with a "-".

Hey Michael,

Please give an example of where the “&” character causes a crash.

I believe the “&” character is reserved in the XML of a plist, so you have to change it to an HTML entity like so:

<dict>
	<key>phrase</key>
	<string>&amp;&amp;expansion</string>
	<key>shortcut</key>
	<string>&amp;&amp;abb</string>
</dict>

I have not tested a transformation to a KM expansion macro with this, but it's what is created when I drag my system text-expansions to the desktop from the macOS > Keyboard > Text prefs and they contain a “&” character.

-Chris

Any text containing & crashed the macro.

For example, setting variable Expansions to the following text

something&somethingelse:12345

yields this error

/var/folders/s4/3q_b_3m90sxc6jbv3qq4yhw00000gn/T/Keyboard-Maestro-Script-3650A9CB-D738-4615-B799-8D4F183F7FE4:123:141: execution error: Keyboard Maestro got an error: a kmmacros file or XML must be supplied (-1)

Not a big problem for me as I just replaced the ampersand with a hyphen.

I'm confused – my macro doesn't contain such a variable...

Are we using different macros then? The one I am using is found at this link:

Create Text Expansions Macro - Macro Library - Keyboard Maestro Discourse

1 Like

Okay, you're referring to Peter's macro – not mine above.

That explains a lot.

1 Like

Yes, the code shown does not encode the input, so you either need to XML encode the input file, or add Filter actions to XML encode the input variables before inserting them in the XML.

1 Like

Apparently, you can just drag and drop the .plist file to the  → System Preferences… → Keyboard → Text pane to import it.

See:

Hello @MacGuy,

While this is useful information, it does not answer the OP's question in any way, shape, or form. That question was:

Is There an Automated Way to Import Apple Keyboard Text Replacement Shortcuts Into KM (aka Keyboard Maestro)?

Please take the time to actually have some comprehension of a topic before commenting on it.

BTW – I did mention that method of export above:

I suppose I should add for posterity that you can select one, some, or all of the abbreviations for export.

--
Best Regards,
Christopher Stone

(Keyboard Maestro Moderator)

1 Like