Jump to content
Macro Express Forums

Nested Macros and Variable Declarations


Recommended Posts

When I create the more complex macros in MacExPro, I make one "main" macro that then calls a series of smaller macros to accomplish a series of tasks.

 

However this also seems to result in problems with variables. Even if you declare and type all your variables at the start of the "main" macro, the "child" macros know nothing about those variables (even if they have been explicitly typed as Global) so you have to declare them all over again in each macro that will use the variable, and try to avoid typos in the process! Helpfully, MacExPro does warn you if you forget to do this. But am I right that there is no way to get around this?

 

More generally, do the more experienced users on this forum agree that building a "modular" sequence of macros is the right way to go when addressing complex processes? Am I creating problems for myself by doing so?

 

When I tried to stuff everything into a single macro, I was having quite a lot of trouble and in particular, it seemed that you cannot nest one "return" loop inside another. That seems rather unusual compared to other languages. Or perhaps I was doing something wrong.

 

Thanks.

Link to comment
Share on other sites

When I create the more complex macros in MacExPro, I make one "main" macro that then calls a series of smaller macros to accomplish a series of tasks.

Good approach.

However this also seems to result in problems with variables. Even if you declare and type all your variables at the start of the "main" macro, the "child" macros know nothing about those variables (even if they have been explicitly typed as Global) so you have to declare them all over again in each macro that will use the variable, and try to avoid typos in the process! Helpfully, MacExPro does warn you if you forget to do this. But am I right that there is no way to get around this?

Try these 2 scripts - they work as I expected.

xx1 initializes 3 variables, displays them, calls xx2, then redisplays them.

xx2 displays the variables, then modifies them, then displays them again.

xx2 displays the same values as xx1, and xx1's second display correctly displays the modified values.

<VARIABLE SET INTEGER Option="\x00" Destination="%nVar1%" Value="125"/>
<VARIABLE SET BOOL Destination="%bVar2%" Command="263" Value="TRUE"/>
<VARIABLE SET STRING Option="\x00" Destination="%tVar3%" Value="This is some text"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3081{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 xx1 1\r\n\\par \r\n\\par nVar1=%nVar1%\r\n\\par bVar2=%bVar2%\r\n\\par tVar3=%tVar3%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<MACRO RUN Use_ID="FALSE" Name="xx2" ID="-1" Wait="TRUE"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3081{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 xx1 2\r\n\\par \r\n\\par nVar1=%nVar1%\r\n\\par bVar2=%bVar2%\r\n\\par tVar3=%tVar3%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3081{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 xx2 1\r\n\\par \r\n\\par nVar1=%nVar1%\r\n\\par bVar2=%bVar2%\r\n\\par tVar3=%tVar3%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%nVar1%" Value="112255"/>
<VARIABLE SET BOOL Destination="%bVar2%" Command="263" Value="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%tVar3%" Value="This is some more text"/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang3081{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 xx2 2\r\n\\par \r\n\\par nVar1=%nVar1%\r\n\\par bVar2=%bVar2%\r\n\\par tVar3=%tVar3%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

More generally, do the more experienced users on this forum agree that building a "modular" sequence of macros is the right way to go when addressing complex processes? Am I creating problems for myself by doing so?

Absolutely the right approach.

When I tried to stuff everything into a single macro, I was having quite a lot of trouble and in particular, it seemed that you cannot nest one "return" loop inside another. That seems rather unusual compared to other languages. Or perhaps I was doing something wrong.

I don't follow. You can have nested Repeat loops without any problems. What am I missing?

Link to comment
Share on other sites

>>Try these 2 scripts - they work as I expected.<<

 

I took your two scripts, created a new macro for each of them, with the macro names xx1 and xx2, and put them in the same Category in the editor.

 

When I run xx1, I get this:

 

Undefined variable or the variable is of the wrong type
%nVar1%
Macro Name xx2
Line Number 4

 

Not sure what I'm getting wrong ... it did ask me several times to define my variables when I tried to save xx2 in the first place (after saving xx1 first). This is precisely what I've been struggling with: I don't seem to be able to get the child macros to work properly unless I re-define the variables that were already defined in the main routine.

 

>>I don't follow. You can have nested Repeat loops without any problems. What am I missing?<<

 

I'm sure I'm just making another elementary blunder in logic or in the order of operations, but here is an example of something that triggers an infinite loop. The examples in my own code are more complex, so I've stripped it down to a bare example:

 

<VARIABLE SET INTEGER Option="\x00" Destination="%x%" Value="1"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%y%" Value="1"/>
<REPEAT UNTIL Variable="%x%" Condition="\x00" Value="5"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%x%" Value1="%x%" Value2="1"/>
<REPEAT UNTIL Variable="%y%" Condition="\x00" Value="10"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%y%" Value1="%y%" Value2="1"/>
<END REPEAT/>
<END REPEAT/>

 

Thanks again.

Link to comment
Share on other sites

Yes, that result looks puzzling at first sight. But the answer lies in Help > Repeat Commands:

Note: The macro commands between the Repeat Until and Repeat End commands are always executed at least once, because the Until condition is not evaluated until after the first iteration.

 

I often resort with head-scratching situations like this to adding a Text Box Display command at key points. Such an amended macro might be:

 

Variable Set Integer %x% to 1

Variable Set Integer %y% to 1

Repeat Until %x% Equals "5"

Text Box Display: 1st (outer) loop

Variable Modify Integer: %x% = %x% + 1

Repeat Until %y% Equals "10"

Variable Modify Integer: %y% = %y% + 1

Text Box Display: 2nd (inner) loop

End Repeat

End Repeat

 

Running that, you quickly realise that because the second loop is being run at least once, it will never stop!

 

Note that even if that logical flaw wasn't present, your macro would have processed only 36 values, not 50, because you started incrementing an initial value of 1 instead of zero

So one way of doing what I think you want would be this:

 

Variable Set Integer %x% to 0

Variable Set Integer %y% to 0

Repeat Until %x% Equals "5"

Variable Modify Integer: %x% = %x% + 1

Text Box Display: 1st (outer) loop

Repeat Until %y% Equals "10"

If Variable %y% Equals "10"

Break

Else

Variable Modify Integer: %y% = %y% + 1

End If

Text Box Display: 2nd (inner) loop

End Repeat

End Repeat

 

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...