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))

11 Likes

Here is a PopClip extension for invoking this macro. You install this extension: https://www.dropbox.com/s/6humlqolbrza7cw/KMsnippetMaker.popclipext.zip?dl=0

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?

Carl

Hey Carl,

Look in the XML of the Export variable.

-Chris

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?

Hi,

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 –. 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, – for –

image

Worked like a charm. Thank you Peter!

1 Like

So glad I found this extremely useful macro! I started with Peter’s since it is newer but had to revert to reboot81’s “ByPasting” version. Not sure if anybody else had issues, but I was getting some“misfires” because of the typing and interference with other Keyboard Maestro macros/Typinator snippets. Example: a long url with the word english (lowercase) in it was rendered EEnglish because I have a snippet that autocapitalizes english→English and I guess because of the typing speed, it got confused and the url broke. Don’t know if others had similar issues with “by typing”. Anyway, it might be nice if Peter’s could be modified to include a “ByPasting” option to avoid this kind of issue.

To change it, just change the “ByTyping” to “ByPasting” in the URL.

Perfect—I didn’t realize it would be that easy. Thanks again!

1 Like

To really know, you'd just have to change one of the created actions to By Pasting and then copy the XML for the action, and then take a look, but they are usually (tho not quite always) obvious - occasionally the internal terminology is not the same as the visible terminology.