New Text Expansion From Selection [using KM8 features]

This is a variant on @peternlewis's Create Text Expansion From Selection macro.

What it does

It creates a new text expansion macro from selected text (as plain text).

The macro runs only with KM 8 or newer.

How to

  1. In any application, select the text you want to create an abbreviation for.
  2. Launch the macro via hotkey
  3. Enter the abbreviation in the user prompt.
    • You'll see the abbreviation field pre-populated with a semicolon ;. This is the default abbreviation prefix and you can change it in the macro.
  4. Make any adjustments if needed. You can adjust this in the user prompt:
    • Insert by…
      • typing [default]
      • pasting
    • Case…
      • sensitive [default]
      • case insensitive
      • case affects expansion
      • regular expression
    • Match…
      • after any character
      • after word break
    • The "Match after" setting will remember your last choice, for the other settings you can change the default in the macro.
  5. Hit Return

A new text expansion macro should now be created.

Extra Features

  • The new text expansion macro will be created in the same macro group where this macro lives. That is, you can drag a copy of this macro into any/each group (for example Global group, BBEdit group, MS-Word group, etc.) to create app-specific text expansion macros on the fly.
  • The name of the expansion macro will be generated from the expansion text. It will be sanitized a bit:
    • Linebreaks are replaced by the ⏎ glyph
    • Tabs are replaced by spaces
    • Length truncated

Note

Initially this macro was just a short test for the new AppleScript classes of the KM-8 Editor (macro, macro group, action, etc.). So, the macro has not seen much real-life testing ;).

Here an excerpt from the contained AppleScript with some of the new classes:

[…]
tell application "Keyboard Maestro"
    tell macro group id groupID
        make new macro with properties {id:macroID, name:macroName}
    end tell
    tell macro id macroID
        make new action with properties {xml:theAction}
        make new trigger with properties {xml:theTrigger}
    end tell
end tell

The macro also uses the new %RandomUUID% token :slight_smile:


New Text Expansion From Selection.kmmacros (15.6 KB)

12 Likes

I am getting a "Invalid XML from Applescript" error on new expansions. Is this a known issue with a known fix?

Don’t know at the moment. Smells like a compatibility issue KM/OS that can be resolved with minor tweaks. I will look into it when I find the time.

Thanks for the note.

1 Like

Much appreciated :slight_smile:

I am sort of moving away from Typinator in order to stick only to Keyboard Maestro and this macro is very useful.

It worked for a while and then it stoped. Now I can't get it to work again.

In the times when I wrote this macro I was trying to do the same :slight_smile:

But it turned out that it somehow didn’t work out. Currently I’m using LaunchBar’s Snippets – via single modifier tap – to insert all my abbreviations; it is as fast as typing a fixed trigger (like with Typinator or KM), and more comfortable, because LB remembers your habitudes. For me.

Aha! Thanks for sharing :slight_smile:
Out of curiosity, why did you decided to move away from typinator? For me it was the fact that it lags for too long when I am adding new snippets or editing them.

I will definitively look into LaunchBar! I didn't pay much attention to it yet, seeing that I use Alfred quite a bit.

I have no issues with Typinator, no lags so far. I just wanted to “streamline” (thin out) my usage of background helper apps (LaunchBar, Typinator, KM Engine). So, I found out that – for me – LB’s Snippets are more comfortable than Typinator’s (or KM’s) typed string triggers. If done via single modifier tap (this is important).


Single modifier taps in the LB preferences:

My CapsLock works as Ctrl (via System Preferences), so it’s a very easy key for the left pinky, to get a snippet/abbreviation.

2 Likes

It’s running fine here, with KM 8.2.4 and macOS 10.14.6.

Which KM/OS version are you using?

Shortly I will upgrade to KM9. I’ll report back if I have any issues there.


PS:

Upgraded to KM9 now, and the macro still runs fine here.

That is odd. I am on KM9 OS 10.45.5.

Question: does it have to be on the Global Macro Group for it to work? It just installed it again and it worked fined. So I moved it to another folder and then error happened again.

No. I tested it in the Global group and in a dedicated “Text Expansions” group, both fine.

Fixed. :upside_down_face:

I use brackets on some folders name (as in [002] Text expansions) so I believe this was causing the error. I removed it and now it works again.

Thank you for your patience and willingness to help out! :slight_smile:

Weird. I tested it in a group “[002] Text expansions” and I don’t get any errors there either.

And, as far as I can see, the group name is not even used in the macro. (The group is set via %ExecutingMacroGroupUUID%)

I switched back the group name and it kept on working. Weird indeed. :face_with_monocle:

Thanks for sharing this!

Can you please share how to automatically open the newly created macro in Keyboard Maestro?

If with “open” you mean ‘open for editing’: You have to open it yourself in KM Editor. The macro is created in the same folder (group) as the creator macro.

If you mean ‘run’: Just run the macro via the typed string trigger you have set for it.

If it doesn’t create a macro, make sure it is in a group that is activated for the app you’re in.

Hi @Tom - I'm essentially looking for a macro that opens the most recently created macro in Keyboard Maestro.

In other words, when I trigger this macro, it should launch Keyboard Maestro and go to the newest created macro (in this case, the text expansion word).

Any thoughts on how to do that? Thanks

I don’t know how to open the latest created macro in general, but in this case I would try this:

In the second to last action of the creator macro you see the variable nteMacroID, which contains the later ID of the macro to be created.

Now, when I look at KM’s AppleScript dictionary I see an editMacro command that seems to take a macro ID as argument:

So, you could simply append an AppleScript action that uses this to open the created macro. (Or append the command directly to the existing AppleScript, inside the KM tell block.)
Maybe with a short delay before.

If you don’t want to open each and every created macro, then you can wrap the action in a condition, for example if Option key is down, etc.

Thanks for pointing me in the right direction!

I tried placing this code inside the KM tell block of the existing Applescript (see below). Unfortunately nothing occurs even when replacing nteMacroID with macro id macroID or macroID.

tell application "Keyboard Maestro"
	tell macro group id groupID
		make new macro with properties {id:macroID, name:macroName}
	end tell
	tell macro id macroID
		make new action with properties {xml:theAction}
		make new trigger with properties {xml:theTrigger}
		
	end tell
delay 5
	
editMacro nteMacroID

end tell

Yes, inside the AppleScript the KM variable nteMacroID is picked up by the variable macroID. (See the tell application "Keyboard Maestro Engine" block towards the beginning of the script.)

I don’t know why it doesn’t work for you. (You said you tested it with macroID too.)

This works for me:

tell application "Keyboard Maestro"
  tell macro group id groupID
    make new macro with properties {id:macroID, name:macroName}
  end tell
  tell macro id macroID
    make new action with properties {xml:theAction}
    make new trigger with properties {xml:theTrigger}
  end tell
  --delay 1
  editMacro macroID
  activate
end tell

The only additions are the 3 lines before the last end tell.

  • For me it works without delay, but this might be different with your setup.
  • The activate brings KM to the front (or launches it).

If it still doesn’t work, check the Engine.log in ~/Library/Logs/Keyboard Maestro/.

1 Like

That worked perfectly! Thanks so much!!