KM8: Local and Instance Variables

That's great. I believe that is a KM 8 enhancement! @peternlewis?

No, I am using macros like that in KM6 as well. Glad that I could teach a KM guru like yourself some minor trick for once. :wink:

Well, there is plenty I can learn about KM, and the Mac in general.

But unless I’m hallucinating, I’m pretty sure Peter has commented on this issue several times before. Are you sure you’re triggering a macro by typed-string while in KM 6?

Hmmm, maybe it was when responding to a KM Prompt . . . can’t be sure now.
I’m sure @peternlewis will be along to set the record straight. :wink:


Yes, absolutely. I have set a typed string trigger macro that types %Variable%% for me and places the cursor before the last percentage sign. And I use it all the time.

Ah, yes, that's actually true, I remember now that I also ran into this problem before.

The environment variables should receive local and instance variables. I'll look in to why that is not happening.

There is no way for AppleScript requests to the engine to reference local or instance variables, they simply do not have the same context. AppleScript can use the environment variables (if it worked) as well (but it is not unicode safe).

Being outside Plane 0 stops them working with various things. I may reconsider, but they are sufficiently broken at the moment that I'm going to remove them for now and think about it more before adding something in. They were a late addition and clearly I did not think through the issues sufficiently.

No, you only can't use Typed String triggers in the Keyboard Maestro Engine. In the editor, they work fine. Nothing about this has changed.

1 Like

Peter, can you provide me with details about how KM executes an AppleScript?

If I have enough details, I'll go to some of the experts in the AppleScript community to see if there is a way to communicate KM instance variables to a Execute AppleScript. In the back of my mind there is something about passing parameters to a AppleScript script.

Keyboard Maestro executes a sub-process that executes osascript that runs the AppleScript.

Thanks for the 8.0.1 update, @peternlewis.

As a note: Local variables (e.g. Local VarName) are still dysfunctional in the shell action (they are not in the env).

While testing this I noticed something strange. I don't know whether this was always like that or it is new in KM8:

I have two variables that are undeleteable: "Password" and "Variable"

The value of "Password" is Password, "Variable" is empty.

These two are not showing up in the environment variable either.

However (the more interesting part): Once I assign a value to the "Variable" variable it appears in the env in two instances:

The first one ("KMVAR_Variable") is expected, but the second one ("KMVAR_Variable_") is not expected. (Note the trailing underscore of the second one.)

Furthermore: When I close and reopen the "Preferences: Variables" window I now see two instances of "Variable" in that window:

When I delete one of them both disappear and a new empty "Variable" variable is created.


I don't have any issues with the strange behavior of that "Variable" variable, I'm posting this just in case that behavior has something to do with the local variables not being sent to the environment.

I have posted a topic to the Wiki Section for an update I made, and to provide a bug report. See:
###Wiki Update: Variables

I have changed 8.0.2, so any variable that starts with “Local” or “Instance” is a local or instance variable (no space required). So you can use space, underscore, double underscore, all of them will be Local/Instance.

It’s plausible this will mean some existing variables will become Local/Instance variables, but it should be a small enough change as to not be significantly impactful.


They should be gone from 8.0.2 (when it is released), if not, please let me know.

1 Like

Thanks, Peter.

What about the single character prefix that @Tom and I suggested:

I dislike verbose variable names. :wink:

1 Like

Yes, @peternlewis, if I look for example at the screenshots of my Retina Macros, which are exclusively using instance/local variables, honestly, I cannot say it is nice to have a five- or eight-letter prefix on each variable! It's clunky.

So, this time, I completely agree with @JMichaelTX.

1 Like

I have not decided on a choice for them, nor weighed the consequences of such a choice. So no, there is currently no short form.

1 Like

This is resolved for the next version (after 8.0.2). Local and Instance variables will be in the environment variables.

Local Variables will work in Prompt For User Input and (non-asynchronous) Custom HTML Prompts.

1 Like

Fixed in Keyboard Maestro 8.0.2.
Please confirm and check "Solved" if fixed.

As of 8.0.3, local and instance variables should work in Prompt For User Input and Custom HTML Input windows.

Also, you can now access them from scripts by using the KMINSTANCE environment variable as a key to which set of variables.

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set v to getvariable "LocalVar" instance inst
	setvariable "LocalOut" instance inst to "FromAS"
end tell

Wow! Many thanks for this, Peter. :+1:

This is huge.
I just confirmed it running Keyboard Maestro 8.0.3 (8.0.3) on macOS 10.11.6.
Also confirmed that running the same script outside of KM does NOT have access to the local/instance variable, which is proper.


Demo of Use of Local and Instance Variables

To improve my understanding of how these variable types work, I wrote the below macro. Hopefully it will help others as well.

Everything appears to work as advertised! :+1:

Please feel free to ask any questions about this.

Example Output

Press OPT-KeyPad1 to Continue BOTH Instances

##Macro Library   Local & Instance Variable Test KM 8.0.3

<a class="attachment" href="/uploads/default/original/2X/1/16c2ab380f0621511815e8339616f677fdce2740.kmmacros">Local & Instance Variable Test KM 8.0.3.kmmacros</a> (15 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**





* **Demo The Use of Local & Instance Variables**
  * Shows that multiple simultaneous executions (instances) of a macro keep both private to that instance.
  * Changing a Variable in one instance does NOT affect the other instance
  * The script run by each instance also sees the proper variable values


1. **KM 8.0.3+**
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink: 

**NOTICE: This macro/script is for _TEST_ Purposes ONLY**


1. Trigger the macro to start the _first_instance
2. Observe the variable values in each Display Window
3. Trigger the macro again to start the _second_ instance.
4. Observe the variable values in each Display Window
5. Press OPT-KeyPad1 to continue BOTH instances.
6. Observe the Script popup Dialog for BOTH
7. Click OK on BOTH dialogs
8. Observe the Display Windows for BOTH instances.


* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  😉
* Assign a Trigger to this maro.  I prefer TBD.
* Move this macro to a Macro Group that is only Active when you need this Macro.
* ENABLE this Macro.

TAGS:  @KM8 @LocalVar @InstanceVar @Script


<img src="/uploads/default/original/2X/1/1bbe1b4b37debc5c25f70e0b664f63f677cb842c.png" width="478" height="1496">

This is great! Many thanks!