Instance Variables Don't Carry Over to Asynchronous Submacros

I have been grappling with a problem and discovered that instance variables are not behaving as described in the KM wiki manual:Variables [Keyboard Maestro Wiki].

The wiki says:
"[Instance Variable] Available to the Macro where it was created and Sub-Macros of that Macro, for a given execution instance of the main macro"

and

"for example, if you execute a macro using the Execute Macro action, that macro can see and change the instance variable"

However, if the submacro is executed asynchronously the instance variable no longer behaves like that. Instead, the scope of the instance variable is restricted to the calling macro and does not encompass the asynchronous submacro.

Here are a couple of macros I used to demonstrate this:

[Test] Macros.kmmacros (3.1 KB)

KM 0 2021-08-25_17-49-53

KM 1 2021-08-25_17-50-46

The first macro sets up an instance variable and then asynchronously executes a submacro after which it displays the value of the instance variable.

The submacro displays the value of the instance variable and then changes its value and exits.

The first macro ends up displaying the value it set up initially - which might actually be valid if the first macro finishes before the submacro has time to change the instance variable's value.

The submacro, however, displays an empty value for the instance variable - meaning it is out of scope.

I can imagine this behaviour is possibly what might be reasonably expected so can @peternlewis confirm this please? If it is intended behaviour, can you please update the wiki before someone else uses up time discovering this?

Personally, I would much prefer if instance variables were in scope of both the calling macro and any submacros it executes asynchronously (or not).

Thanks!

@peternlewis?

Correct. An asynchronously executed macro is a new instance.

You can send the ExecutingInstance to the new macro and access variables via that, but the new asynchronous execution is its own new instance.

1 Like

Just to be absolutely clear @peternlewis,

of itself. It’s no different to running the submacro by clicking the Run button in the KM editor for example. Except of course you can pass a parameter.

If that is correct, then please update the wiki to properly reflect that in the description of instance variables. Just so others aren’t caught out and waste time like I did. Thanks!

Correct, it is as if the newly executed macro was triggered by some means (Run in the Editor, a hot key, whatever).

Both macros are running simultaneously, the new one as a new execution instance.

I have clarified the documentation on Instance Variables to note the exclusion.

1 Like

Thanks. A small change to the documentation, I know, but very significant!