TIP: How To Determine if Macro is Used a Sub-Macro, Called by a Execute Macro Action

Use Case

  • AFAIK, KM does NOT provide a direct means of determining when a Macro is called via a Execute a Macro action, aka a "Sub-Macro"
  • So if a Macro could be used directly (i.e. executed by a trigger), OR it could be used as a Sub-Macro, how can we tell how it is being used?
  • After some limited testing, it appears to me that:

IF a Macro is triggered directly, THEN:

  • The %ExecutingMacro% token will be the SAME as the %ExecutingThisMacro% token.
  • The %TriggerValue% will be based on the actual trigger

ELSE IF a Macro is Called via "Execute Macro" Action, aka "Sub-Macro", THEN:

  • The %ExecutingMacro% token will be DIFFERENT from the %ExecutingThisMacro% token.
  • The %TriggerValue% will be the PARAMETER as entered on the Execute Macro Action.

@peternlewis, would you please confirm or correct this assertion?

Assuming this is correct, here is a Macro that shows how to use this information.
It is a Macro that can be used in either mode (Direct or Sub-Macro).

Example Output

Trigger Tokens When Used as a Sub-Macro

Trigger Tokens When Used Directly (Triggered by a HotKey)

image

MACRO:   Trigger Tokens in a Sub-Macro (called by Execute Macro)

VER: 1.0    2020-01-05
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Trigger Tokens in a Sub-Macro (called by Execute Macro).kmmacros (11.2 KB)

Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


MACRO:   MAIN Macro to Call Sub-Macro To Get Trigger Info [Example]

VER: 1.0    2020-01-05
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

MAIN Macro to Call Sub-Macro To Get Trigger Info [Example].kmmacros (3.7 KB)

Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


2 Likes

It is correct, except the last line:

  • The %TriggerValue% will be the PARAMETER as entered on the Execute Macro Action.

The %TriggerValue% will be the PARAMETER as entered on the Execute Macro Action if it uses a parameter. Otherwise (if the Execute Macro action does not include a parameter) the %TriggerValue% will be inherited from whatever it was in the parent macro.

1 Like

Thanks for the clarification, but I DID say "the PARAMETER ==as entered== on the Execute Macro Action"

So, I believe my statement is correct as written. :wink:
It just did not cover the case where the parameter is not entered.