Macro to Create a Text Expansion Macro from the Selected Text

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!

Hi everyone,

that's a great and helpful script. Question: is there a way to make it work, that the "Text" includes "/", for example "John / Doe".

What do I have to change?

That would be great. Thank you so much!

The macro works as is with text that includes slashes (/) in it.

Thanks Peter. It seems I was using a different version. Two different were recommend to me in a different thread. I thought I used yours as a trustful source and in the other one it did not work.

Thanks for the help and quick clarification!

Hi Peter, this is such a great macro. Unfortunately for me it seems to fail when the selected text is in Microsoft Word and includes the "–" dash that replaces a "-" when typing. I tried first using Remove Styles on the Text variable to no avail. E.g.,

Here's some text – and some text following the dash.

Throws an "Invalid XML From AppleScript" error.

Presumably the XML does not accept the HTML entities, such as &ndash;. Keyboard Maestro does not have a filter that only produces numeric entities, so you will just have to do that yourself for any entities you encounter, add a Search & Replace the Text Variable, &ndash; for &#8211;


Worked like a charm. Thank you Peter!

1 Like