How might I save a variable between Applescript runs using Execute Applescript in KM?

Hello everyone!

How might I save a variable between Applescript runs using Execute Applescript action in KM editor?

Use the setvariable method of

application "Keyboard Maestro Engine"

AppleScript [Keyboard Maestro Wiki]

and then the getvariable method to retrieve a value in a subsequent session.

You can add a "property" declaration at the start of an applescript and it will save the results between applescript runs, but it won't work if executing AppleScript from KM editor.

property testVar : 1

if testVar < 5 then set testVar to testVar + 1

return testVar

Just test the script above using Script Editor & KM Editor and see the difference.

Probably @peternlewis will make it clearer for us.

The value set by a property definition is not reset each time the script is run; instead, it persists until the script is recompiled. - As far as I understand, KM recompiles applescripts before running them and this method can't be used to store results between runs or at least I couldn't find an option to disable recompiling and switched to FastScript as the property method works perfectly with FS.

If it is a compiled script file that you are executing, then you can use properties like this.

If it is a text file or text in Keyboard Maestro, then it is recompiled each time and so the property would be reset each time.

1 Like