New Text Expansion From Selection [using KM8 features]

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

Glad that it works :slight_smile:

May I ask what is the purpose of opening/editing every macro immediately after creation?

I’m asking because, if it is for example a specific setting/option you are missing in the prompt window, then it would probably be easier to add that option to the prompt, instead of setting it afterwards in the created macro each time.

Haha - mostly just a neurotic need on my end to validate that the macro actually worked.

Having it as a setting would be nice, but as-is, also works.

Thanks so much for your time Tom!

OK. So you will delete the 3 new lines of code in the near future, I guess :smile:

No, what I meant with “setting” was whether an important setting is missing in the prompt (when you create the expansion macro).

In the prompt, you can set the trigger, the case sensitivity, etc. But, for example, a thing you cannot set (in the current version of the macro) is the “simulate n deletes” setting. (I didn’t include that, because I think in 98% of the cases you would likely want to have that activated.).

And hence I thought the reason for your editing desire was some missing setting in the prompt (either the simulate deletes or some other other thing I’m not aware). But every option that is available in the KM macro trigger of an expansion macro, can also be implemented in the prompt.

Well, if it’s only for neurotic reasons, then … feel free to keep that neurosis up and running with the help of our modifications :relaxed: :slightly_smiling_face:

lol - sounds good!

What does sound good? (I just made some corrections/additions to my post above; maybe you missed them. Reload the page in that case.)

Hi Tom - I don't have anything else to add to your corrections/additions :grinning:

Everything "sounds good" , in other words :+1:

This macro is perfect for me with the new code you shared, thanks again for your time!

No prob. Glad that you are happy with the macro now!

PS:

I have seen I have a more recent version of the macro in my KM library, which I have labeled as “beta”. (But still from 2018, it seems.) Trying to figure out what the improvements are (besides making some variables local).

Maybe I’ll post that. Or, in any case, if you come across issues with the posted version, let me know, and I will give priority to a revised version.

1 Like

This is a great macro but I have also found the 'Invalid XML From AppleScript' problem. In my case, it appears to result from incompatible characters in the captured string.

To be specific, £ and both break the XML. E.g.:

Probably some other characters too, I just tested everything on my number row.

I've been using a snippet-making macro that doesn't rely on creating a new macro for each snippet, which gets around this problem. It looks to a .tsv database instead. However, the 'one macro per snippet' method also has its advantages.

Another thing I've found is that concluding each snippet-triggering macro with a 'Clear Typed String Buffer' action can make subsequent snippets trigger more reliably. That is the one main advantage that I've found Typinator to have over KM: it never fails to trigger, whereas KM occasionally does.