Suggestion for the AppleScript Page Re: Local/Instance Variables

This page covers the use of variables in AppleScript, and states plainly that you can use both local and instance variables. And you can… as long as your AppleScript isn't in a macro that's executing asynchronously.

I assume this is because an asynchronous macro is its own instance, so the variables you're trying to use just don't exist there. This makes sense, but it would have saved me a lot of time if that page mentioned that you can only use local and instance variables if the macro is running synchronously.

-rob.

Hmmm...... this would make sense and explain some issues I was having with a macro I was running as a asynchronous subroutine and used instance variables in an AppleScript. The script would not read nor return the variables to the calling macro.

Perhaps @peternlewis can confirm if this is the case, and maybe the wiki could be updated to show this detail?

-Chris

I just tested this and it works fine for me.

Here's the macro that executes another asynchronously
KM 0 2021-12-28_17-39-52

This is the sub macro that executes asynchronously
KM 1 2021-12-28_17-40-19

It displays the contents of "LocalVar" properly.

If this doesn't reflect the way you are running your macros, you need to show us exactly what you're doing.

I can't set the instance variable in the submacro, it has to be set in the main macro, because it depends on calculations. So try it this way:

Main macro:
• Set variable instanceVariable to "testing"
• Call macro DoAppleScript, async

DoAppleScript macro:
• Set AS variable based on instance variable
• Display dialog AS variable

For me, it's blank. Uncheck async in the main macro, and it has a value.

-rob.

Then instance variables from an AppleScript can be passed to the calling macro even if the subroutine is running asynchronously.... hmm.... no doubt the issue was with the macro I was attempting to build, which I deleted :laughing:

Here's a simple test case. Run Main it as is, it fails. Disable async, and it works.

___ . test macros ___ . Macros.kmmacros (3.1 KB)

-rob.

This is partially my fault: Anything async gets nothing from the calling macro, AppleScript or not. Which I understand now—it's async, separate process, no sharing. But still, I read that page and assumed I could pass local/instance variables.

Net net, anything async needs globals.

I still think a note on that page would be useful :).

-rob.

I see where you’re coming from, but this thread might be of some help:

If you pass the instance of the calling macro to the asynchronous sub macro it would seem you can access those instance variables…

4 Likes

Ah ha! Yes, that would be the key to success…but honestly, having to create an use a separate instance variable, versus just creating then deleting some globals, I guess I'll just stick with the globals.

But thanks for that link—bookmarked for future needs!

-rob.

Maybe that AppleScript page needs a link to that forum topic in the section about local and instance variables—that would've helped, too, when I was trying to figure out what was going on.

-rob.

Amen to that brother :sweat_smile:

1 Like

Yes, any asynchronous execution will result in a completely new instance, the same as if the submacro had been afresh.

2 Likes

Hey Rob,

You may be right for your use-case, but it seems to me you can pass the %ExecutingInstance% token as a parameter and not have to create a variable in MAIN.

-Chris

1 Like

Ah, good point; I'll give that a test run—thanks!

-rob.

1 Like