How Do We Use %ExecutingInstance% token?

@peternlewis,

Many thanks for providing these tokens:

%ExecutingInstance% token

The %ExecutingInstance% token returns (v9.0+) an opaque ID of the currently executing instance, including both the execution instance and the specific macro executing. It can be used to retrieve Local and Instance variables via AppleScript.

Sounds fantastic!
But how do we use it?
Could you please provide a scripting example.

Thanks.

Hmmm, I think in Applescripts instead of the %ExecutingInstance% token you use the environment variable KMINSTANCE instead?

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   set kmLocalVar1 to getvariable "Local__SomeLocalVariable" instance kmInst
   set kmLocalVar2 to getvariable "Instance__SomeInstanceVariable" instance kmInst
   setvariable "Local__FromAS" instance kmInst to "Variable set in AppleScript."
end tell

I've always assumed the token and the environment variable return the same thing, for the same instance.

I have used the %ExecutingInstance% token where if I ran a macro a second time, if a first instance was running I wanted to kill the first one, not the second one.

Example Macro

This example pauses a macro for 5 minutes. If a new instance is run during the 5 minutes, the original macro is cancelled and a new 5 minute timer started.

Macro Download

Executing Instance Example.kmmacros (2.8 KB)

1 Like

Yeah, did a quickie test and the AppleScript environment variable is the same as the token.

2 Likes

Oh I know what Peter meant now. Duh.

Pass in the value of an executing instance FROM A DIFFERENT INSTANCE and you can retrieve the values from that instance too.

2 macros below. First saves it's instance in a global variable. Sets an instance variable named Instance_MacroName to it's name.

Second set's the same variable name Instance_MacroName to it's own name, then calls an AppleScript. That AppleScript pulls the value of Instance_MacroName from the other Instance and from the Instance that called it and then sets some local variables (in the calling script) with the values. Values are displayed in a window and the first instance is cancelled.

Macro 1

Macro 2

Results

Macro Downloads

Instance Macro Examples.kmmacros (5.0 KB)

(Edit: forgot the results)

4 Likes

For All: See: