Local variables not displayable in Prompt for User Input Actions?

Is it to be expected that we can't use a Local variable to display a default value in a Prompt for User Input Action ?

I had expected to be able to display the previous user choice (current value of a Local variable) as a default in the case below. It works with global variables, but no value seems to be displayed in the form if the variable is Local. Does the value of a Local variable not persist between calls to its host macro ?

( for working example, see this macro)

(Not a restriction that I see mentioned in the Manual)

If I understand local variables, then they are deleted once the macro are run.

So there is now way of displaying it on a subsequent run of the macro.

Or than saving it to a global/persistent variable.

Thanks, yes – I was relying too much on the table at

https://wiki.keyboardmaestro.com/manual/Variables

in which the restriction to a single execution sequence is mentioned in relation to Instance but not Local variables. I notice now that a paragraph lower down mentions that Local variables also don’t persist across instances.

I’m sure there are good design reasons for that, though I do miss that empty quadrant in the cartesian product of (persistent ⇄ non-persistent) * (local ⇄ global)

Persistent memory visible only to one macro would be useful, and I had thought that that was the point of distinction between Local and Instance, but my impression now is that Local is actually more restricted than Instance rather than less ?

Is this the picture ?

I think so. Have only just begun to change my macros to use the these different types of variables.

To fully understand Local/Instance macros, you have to understand that when you trigger a macro, the Keyboard Maestro Engine takes a copy of the macro and starts an execution instance to run that macro. Each time you Execute a Macro within that, Keyboard Maestro Engine takes a copy of the macro and starts a sub-execution instance to run that macro.

Local Variables are specific to a single sub-executation instance. So Local variables will be fresh and empty at the start of every macro (and sub-macro) and will no be passed into any sub-macro, nor will any value that comes back be relevant to the macro. Local variables are very local. Even the same macro will not see any values set by a sub-execution of the same macro.

Instance Variables are specific to a single combined execution instance. So Instance Variables will be fresh t the start of a macro that is triggered by something, and will be passed down into sub-macros executed by that macro, and changes made to them in sub-macros will be applicable to the parent macros. Instance Variables are local to a single execution instance, but across sub-macros within that execution.

Note that executions outside of Keyboard Maestro (eg scripts) and asynchronous executions (eg Execute Macro asynchronously) start new execution instances and so no longer have access to Local/Instance variables (sometimes they are available by jumping through hoops to access them specifically).

So it is not so much about persistence, it is about restrictions of scope (although it makes no sense for them to perish because the scope only lasts for a period of time and then no longer exists).

As far as your diagram, a variable that both persists, but is local to a specific macro, but across all execution instances of that macro, there is no such concept in Keyboard Maestro. If that is what you want, add the macro name (or some other unique identifier) to the variable name.

1 Like

Peter, thanks for providing more details. I think many of us continue to struggle with the scope of Local and Instance Variables.

So, some more questions to help clarify the scope:

  1. Multiple Executions of Same Macro
    β€’ If the same Macro (not concerned with sub-macros here) gets triggered two or more times so that all of the executions (instances?) are running at the same time, does each execution have its own set of both Local and Instance variables independent for all the others, or if a Local or Instance variable is changed in one execution (while all are running), then the changed value is immediately set in all executions? Is there a difference in how Local and Instance Variables would behave in this scenario?
    .
  2. Scripts
  • If I have scripts in this macro that use a Local or Instance variable, does the script called by each separate execution of the macro have its own copy of the variable?
    .
  1. Local vs Instance variable
  • Is the only difference between Local or Instance variables their scope in sub-macros?

Yes. Each independently triggered macro is a separate execution instance, with its on Instance Variables. Local Variables are also independent, but they are independent anyway for each time you start a macro whether it is in the same execution instance or not.

Local Variables are always independent for each and every time a macro is executed.

Instance Variables are independent in they are accessed in different execution instances, that is, if they come from two different macro trigger events.

Scripts get variables read-only vie the environment, so when a script is executed, it gets a copied of the applicable Instance variables (for this macro trigger instance) and the appropriate Local variables (for this specific macro for this specific execution of that macro).

Using the KMINSTANCE environment variable (which includes data about those two states - the execution instance for the Instance variables and the macro execution for the Local Variables), you can access the Instance and Local variables by "jumping through hoops" in the form of passing that information back to Keyboard Maestro Engine when accessing variables via AppleEvents.

Yes. Think of it this way:

  • An execution instance starts each time you trigger a macro. You get a new set of Instance Variables at this point.
  • A macro starts each time, well, each time a macro starts by any method. You get a new set of Local Variables at this point.

As mentioned, executing a macro asynchronously is equivalent to triggering it (so it gets a new set of Instance Variables (and a new set of Local Variables, but that is always the case for every time a macro starts)).

1 Like