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.
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?
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
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 :).
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!
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.
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.