[Tip] A Type a Key action can trigger another macro

I just discovered, partly out of need and partly out of curiosity, that a Type a Key action in one macro can activate another macro. For the purposes of this discussion let’s call the activated trigger KEY and the macro that types it METAKEY. [KM often turns out to be meta-powerful – what it can do is just miimd-boggling.]

Why would you want this? The particular case I needed this for is one where a keystroke is bound to different macros depending on which application is active, as specified in the Available conditions for groups (for instance, a group for browsers or a group for text editors).

Suppose you had a Move to End of Paragraph macro defined differently in different groups, but each triggered by the same KEY. You want to move to the end of the paragraph as a step in a macro in the global group regardless of what application is active, so you put in a step that types KEY and bind the macro to METAKEY.

What this saves is a deeply nested set of if-then-else actions, where a different macro is executed depending on which application is active (or whatever other conditions are specified in the if conditions). The only requirement is that each of the macros used are bound to the same key within their group.

I know this is weird. I’m working on some macros where this is actually useful. If any of them would make any sense out of context I’ll post them to show more concretely what I’m talking about.

Please note that it is not well defined as to whether a Keyboard Maestro typed keystroke will or wont trigger a macro with the same hot key.

Keyboard Maestro actually tries to avoid this happening, but due to the nature of the event queue, it is impossible to do robustly one way or the other.

So using this sort of feature is generally not advised. It’s behaviour is likely to be inconsistent.

Aw shucks. I should have asked you privately first. Sorry. Too bad – it was just so cool, and I really do have a couple of uses for it. Oh well, deeply nested conditionals it will have to be.

I'm thinking out loud here, and this may not be better than a bunch of if-then-elses, but I'll throw it out there.

The basic idea is this: For each relevant application, when the application is activated, you set a variable indicating what macro to run when the time comes. Then, in the place where you would do if-then-elses, you just execute the macro specified in the variable you set earlier.

So let's say that when a particular application is activated, you set a variable called, for the purposes of this example, "MyMacroUUID". (You can copy a macro "as UUID", to get this value.) So it would look like this:

You do this for each application that you might want to execute.

When it comes time to execute the macro, you do something like this:

OK, so now that I've finished this, I realize it's probably not as good as if-then-elses. But since I bothered to think it out, I'll post this anyway. :stuck_out_tongue_winking_eye:

No, I love it!

I have never set up a dict action, but it’s great. Is this common? Information posted somewhere?

What IDs are you using and where did they come from?

Oh, it’s not a “dict” action - that’s just the first line in the file. LOL - took me a minute to figure out what you were saying! We’re just setting a variable to this text. Peter (the program’s author) posted something for me a week or two ago, that had code like this in it.

In this case, the UUID comes from selecting the macro, then going to Edit->Copy As->Copy As UUID. The program actually uses theses kinds of IDs internally to identify macros. That’s why renaming a macro doesn’t hurt other macros that use them in an “Execute a Macro” action.

As for the “dict” code itself, it’s actually an XML snippet. I dropped an “Execute Macro” action into a macro, selected the action, and then did File->Export Actions to export the action to disk, then opened the file in a text editor.

You only need the part between the “dict” elements. An odd quirk is that the opening and closing “dict” elements must be at the start of a line - no spaces or tabs before them, otherwise the don’t work in this situation.

No need for the XML in this case.

The AppleScript do script command takes one of:

  • A macro name
  • A macro UUID
  • An action XML

@DanThomas creates an XML for an Execute Macro action to execute based on the UUID, but there is no need for that, just use

do script uuid

or in this case

tell app "Keyboard Maestro Engine"
	set v to value of variable "McMacroUUID"
	do script v
end tell
1 Like

Awesome! Thanks.

Do you have any documentation on what we can do with AppleScript like this?

Sure, see the Scripting section of the documentation.