[SOLVED] Understanding variables in AppleScript once and for all

I always felt that the documentation is a bit confusing, especially for someone new to AppleScript like myself.
I think it could have real examples with screenshots of macros instead of just the scripts.

So today I decided to read it again and take my own notes. Can someone check if this is the way it works?

Global variables

Get global variable myKMvar from KM > Process it via "Execute AppleScript" action by creating the AppleScript variable myAppleScriptVar > No output variable

tell application "Keyboard Maestro Engine"
	set myAppleScriptVar to getvariable "myKMvar"
end tell

--- my script here

Create AppleScript variable myAppleScriptVar via "Execute AppleScript" action > Send it to KM and save it as a global variable myKMvar

set myAppleScriptVar to "12345"

tell application "Keyboard Maestro Engine" to setvariable "myKMvar" to myAppleScriptVar

Local variables

Get local variable from KM > Process it via "Execute AppleScript" action > No output variable

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set myAppleScriptVar to getvariable "Local__myVar" instance kmInst
end tell

--- my script here

Create AppleScript variable myAppleScriptVar via "Execute AppleScript" action > Send it to KM and save it as a local variable Local__myKMvar

set myAppleScriptVar to "12345"

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine" to setvariable "Local__myKMvar" instance kmInst to myAppleScriptVar

Source: https://wiki.keyboardmaestro.com/AppleScript

That’s all accurate. Also, instance variables work the same as local variables in the AppleScript environment. Also, fwiw, you can retrieve variables from KM using one-liners in the same way your examples show setting KM variables (for instance and local you do still need the set kmInst to system attribute "KMINSTANCE" part however).

So to retrieve would be...

tell application "Keyboard Maestro Engine" to set myAppleScriptVar to getvariable "myKMvar"

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine" to set myAppleScriptVar to getvariable "Local__myVar" instance kmInst
1 Like

As a footnote, reading Keyboard Maestro variables in JavaScript for Automation has become much easier than reading them in AppleScript, as of Keyboard Maestro 11.

(You just prefix KM variable names, local or global alike, with kmvar.)

action:Execute a JavaScript For Automation [Keyboard Maestro Wiki]

If you are starting off with scripting, JS may, these days, prove to be a more rewarding investment of time than AppleScript, which I find I seldom use now (too fiddly, too much missing).


I still haven't fully researched instance variables, so I still don't know when to use them. So far, using just local and global variables has been working for my macros, but I definitely want to take some time to read about instance variables.

Yeah, I was thinking about that as well. Actually, it was you who told me about that recently (and I see that you replied to that topic I started today about that).

I need to get used to it. I don't use AS a lot to really feel "at home", but I've been learning here and there with the macros I build, so some things are becoming more familiar. :slight_smile:

I'm not comfortable with neither yet, but it seems that certain things require AS to manipulate elements that maybe Javascript wouldn't?
As a newbie, I pretty much just go with the flow and it seems that for certain tasks, most people here on the forum seem to use AS, I guess.

I don't use those languages extensively, so I tend to learn the most "popular" here on the forum, and because I don't need to use them on a daily basis, it's hard to get super familiar with everything. I learn here and there as I go. I even have some favorite actions with some pre made AS to make it easier to use

The Variables wiki page has all the answers to that. But it is sort of overly-detailed for a top-level understanding, so here's a hopefully simple explanation and example of the differences between the two.

Instance variables are available anywhere within the execution of the macro that created them, whereas local variables are only available to the macro in which they're defined.

Let's say you have a macro Foo, and in its code somewhere, it calls macro Bar. If you set local_var in Foo, Bar won't be able to see that variable. If you set instance_var in Foo, Bar will be able to see and use that variable.

Because I'm never sure how I'll use a variable when I'm developing a more complex macro, I tend to use instance_ variables in most spots, just so I don't have to go back and change them if I decide I need to access them from another macro within the main macro. I generally restrict local_ variables to things like counters for loops and other such definitely-not-used-elsewhere tasks.

Both instance and local will be cleared at the end of the run.


1 Like

Not really, they are peer languages – two different ways of interacting with the same scripting interface:

Mac Automation Scripting Guide: About Mac Scripting

Unlike AppleScript, JS can also be used on iOS and on the web, so any hours of experimentation tend to harvest larger and more durable rewards.

1 Like

I thought AS was the main language and so it would be easier to use it when dealing with certain elements.
Thanks for clarifying and for the link.

Thanks for clarifying that.
So my question is: if they do the same thing and are both cleared at the end, but the instances is more flexible, why use local at all?
Wouldn't it make more sense to just use instance variables?

I am certainly not an authority on the matter, and my comment here is based on just my personal testing and observations, but local variables appear to execute slightly faster than instance, and since they are not accessible outside of their own macro, there’s no danger of them being overwritten by a different macro that might be executing at the same time. This latter part is especially important if you, like me, have a bunch of favorite actions, or templates, that make use of standardized variable syntax.

Again, that’s all based on my own experience, nothing more. YMMV :wink:

1 Like

Same here. When I first started using KM, I wrote everything with globals. Then I started using more instance ... and now I'm migrating to almost all locals except where I absolutely need an instance or a global. (I wrote that I use instance variables by default, but that's only in complex macros where there's a good chance I'll have subroutines, etc. that may need them. For everything else, it's local.)



I can see how that could be an issue. Thanks for pointing that out.
Again, I never used instance variables so local variables have been the ones I use 99.9% of the time. I only use global variables when it's really necessary, where local won't do the trick.

I would like to believe that we all followed that path :wink: