Create Text Expansion From Selection Macro

Here is a macro for creating a new text expansion macro from the selected text. Select the desired text, trigger the macro (by default it is with a Status Menu trigger, adjust as desired), enter the trigger text when prompted and the macro will be created.

Updated using version 8.x AppleScript facilities.

Note that although it attempts to encode the HTML entities, the requirement is that it actually be XML entities, and so it will not necessarily work well for non-ASCII text.

Create Text Expansion From Selection.kmmacros (4.7 KB)

(Old version for pre-8.x: Create Text Expansion From Selection.kmmacros (5.3 KB))


Here is a PopClip extension for invoking this macro. You install this extension:

Then install the above macro. Select some text and select “KM” on PopClip. That replaces my TextExpander extension quite nicely.

I imported your macro and then moved it to a different macro group expecting the new text expansion macro to be placed in that same macro group, but it appeared in the “Text Expansions” macro group (even though I deleted this macro group after moving your macro; KM recreated the macro group). How does KM decide where to put the new text expansion macro and can it be changed?


Hey Carl,

Look in the XML of the Export variable.


1 Like

Thanks Chris.

This is truly a neat macro. However, I prefer "ByPasting". So I tweaked it a little.

Based on @peternlewis's "Create Text Expansion From Selection Macro"

  • Changed ByTyping to ByPasting.
  • Added an action to set system clipboard to previous state.
  • Changed "match any character" to "match after a word break".

Create Text Expansion From Selection v2.kmmacros (5.9 KB)

1 Like

I have updated this macro to use the new Keyboard Maestro 8 AppleScript facilities so an export file is no longer needed.

1 Like

Hey raguay

Installed the PopClip extension and can select KM from PopClip but then nothing happens. Is this still working with Mojave10.14.1 and KM 8.2.4?


It works fine for me on Mojave with the latest KM and PopClip. But, you have to make sure KM and PopClip have complete access to other applications in the Security & Privacy → Privacy section. The os should of ask for permission the first time you ran it on Mojave, but if you missed it and accidentally told it no (I did the first time), then you have to manually add the permissions.

Thanks Tanguay, good to know that it's something with my system.
Both apps have full access.

Also, I assume this only adds a new triggering option and the same interface comes up and the new macro is saved within the same macro group where the original macro is located.

I'm running a days long process and will restart as soon as it's done and update this if that does or doesn't work.

Thanks again!