Understanding Local vs. Instance Variables

I am trying to put together macros(s) to compile documents in Pages. I think that I'm missing something when it comes to Local and Instance Variables. I have a macro that creates the shell template for the document. I set certain variables to text in the macro. I then execute another macro within that macro, with the hopes that it will use the variables previously set to text within the sub macro. It's not working, and I'm not sure what to do from here. Is there some resource out there that breaks down the distinctions between Local and Instance Variables? Thanks in advance.

They are described in the Variables user manual section.

An “instance” is a single execution, but may cover multiple macros. So if you trigger a macro, and it uses an Execute a Macro action, both the initial macro and the executed sub-macro are within the same “instance”. They share instance variables.

A “local” variable is specific to a single macro, single instance. So in the above case, both of those macros would have their own independent local variables.

Thank you, sir. Works perfectly now.

@Martin_B_W, I hope you don't mind me piggy-backing on your excellent question, even though it was over 6 months ago. I have a question that I think is directly related.

@peternlewis, I thought I had finally internalized the difference between Local and Instance variables, but this question now occurs to me:

Is there any reason to not always use Instance variables, including where I previously would have used Local variables?

IOW, are there any cons, any downsides, to just always using Instance variables?

IAC, thanks again Peter for giving us these powerful variable types.

Here's My Logic/Use Case

  • Sub-Macros can only use Instance variables to share the variable with the main, calling Macro.
  • Whereas, scripts can use either Local OR Instance variables to share with the calling Macro.
  • So, it seems to me that always using Instance variables will simplify my designs and code, and make future mods easier.

2 posts were split to a new topic: The Important Distinction Between Local and Instance Variables

@JMichaelTX, I don't mind at all. Peter's response was incredibly detailed. I'll be saving that for reference later.

1 Like