Condition for whether a macro is being run from within another macro

I want to test whether a macro is being run from within another macro (using the Execute a Macro action). Is there a condition I can use for that? (And, ideally, a variable or token that identifies the Macro from which it’s being run?)

I test the trigger value. If there is one, the macro isn’t being called. If there isn’t one, it is.

KM does not have a call stack, so you will have to build your own using KM Variables. You can pass a parameter in the Execute Macro that is the name of the calling macro.

So, you could do something like this:

  1. Macro A calls (Execute Macro) Macro B, with a parameter of %ExecutingMacro%
  2. Macro B sets a KM Variable “MacroBStatus” to ADD a line using the parameter
  3. Just before Macro B finishes, it edits MacroBStatus to remove the line with the parameter. (RegEx can easily do this).
  4. Then any Macro can check the value of MacroBStatus to determine if it is in use. (If it is empty, then it is not in use).

If you are already using the parameter for Execute Macro, then Macro A will have to the edits to MacroBStatus.

If this doesn’t make sense, feel free to ask questions.

Also, you might be interested in the Semaphore Lock action (KM Wiki) that will prevent simultaneous executions of the same macro.

Why?

My question is because you’d probably have an easier and clearer job if instead of something like this:

submacro: do something or something different if executed directly
macro: do stuff and call submacro

and instead had:

submacro: do something, perhaps varying behaviour based on the TriggerValue
macro1: do stuff and call submacro
macro2: do stuff and call submacro

Yup, that makes sense. I ended up taking the submacro apart and executing the steps directly (with slight differences) from two different macros. I thought using a submacro might simplify things but it was actually making them more complicated.

In my case, I wanted to be able to run the macro independently but also call it as part of a sequence. It simply writes the current open file to a backup.

When I run it independently, I want a notification to tell me it finished. When it is called by another macro, I want it to run silently.

(This, BTW, is the Backup macro in the Minify CSS/JS palette.)

So by checking for the existence of a trigger value, I know whether it was called independently (true if there is one) or from another macro (no trigger value).

Very simple and reliable.

Yes, this is why I would just do it the way I suggested. The macro you run directly then just does: Execute Macro, Notify.

But whatever works for you is obviously fine.