Looping Problem

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Looping Problem

Post by RNIB » Thu May 21, 2009 2:22 pm

With the help of some very kind people on here I have created the following macro:

Code: Select all

Label>Start
//Ask how many times to run
Input>NumTimes, How Many Pages Do You Want To Add:,10
Let>x=0
Repeat>x
Let>WW_TIMEOUT=30
//Switch to EasePublisher
SetFocus>EasePublisher
WaitWindowOpen>EasePublisher
//Move to end of Waveform
Press Alt
Press End
Release Alt
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=100
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>1
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
//Open Add Page Dialogue
Press CTRL
Send>1
Release CTRL
WaitWindowOpen>Add page(s)
Endif
//Enter automatic page number
Wait>1
Press Enter
WaitWindowClosed>Add page(s)
SetFocus>EasePublisher
WaitWindowOpen>EasePublisher
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=100
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>1
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
SetFocus>EasePublisher
//Move to next file in list
Press CTRL
Press Page Down
Release CTRL
//Get macro to wait until waveform fully loaded
GetScreenRes>sX,sY
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Let>maxwait=100
Let>counter=0
Repeat>imgs
Let>counter=counter+1
if>counter=maxwait
exit>
endif
wait>1
FindImagePos>C:\temp\loadingaudio.bmp,SCREEN,0,0,XArr,YArr,imgs
Until>imgs=0
Let>x=x+1
if>x=NumTimes
Ask>Complete! Would You Like To Add More Pages?,strResponse
endif
if>strResponse=YES
Goto>Start
endif
if>strResponse=NO
exit>
endif
Until>x=NumTimes

When I launch the macro it asks me how many times I want it to run and whatever number I enter here it will correctly run that many times without problem. Once it's finished it then asks me if I want to add more pages and this is where the problem comes in.

If I say no the macro exits correctly, I can then relaunch it and ask it run another xx times and it will do this correctly again.

However if I say YES to adding more pages, no matter what number I enter the macro will ONLY run once. Once it has run that one more time I get another message asking if I want to add more pages and at this point no matter whether I click on No, the cross in the top right corner or enter 0 the macro continually runs one more time and so it becomes impossible to quit as that just makes it run once more again and so on and so on.

For the life of me I can't figure out why this is happening, any ideas?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu May 21, 2009 3:37 pm

The problem is that you have two conflicting ways to close the loop:

if>x=NumTimes

and

Until>x=NumTimes

So replace the if with the until and I think it should be OK


Code: Select all

//end section of code
Let>x=x+1

//replace the if here with the until
Until>x=NumTimes
Ask>Complete! Would You Like To Add More Pages?,strResponse
endif
if>strResponse=YES
Goto>Start
endif
if>strResponse=NO
exit>
endif
//delete the Until> here

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu May 21, 2009 3:41 pm

Add
Let>strResponse=
after the Label>Start line

I agree with me_again that you have some logic that I wouldn't use but I think the issue you're reporting is that the variable "strResponse" is set to "YES" and stays that way.

Another logic issue that seems to be working for you but I would not do is multiple repeats using the same variable name.

I also would not use the single letter "x" (or any other single character) as a variable.

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Thu May 21, 2009 3:47 pm

The problem is that you have two conflicting ways to close the loop:

if>x=NumTimes

and

Until>x=NumTimes

So replace the if with the until and I think it should be OK


Ahh yes of course!

I'd been staring at that for ages and just couldn't get my head around it.

Thank you so much, I really appreciate it. You've saved me from going bald(er) :wink: [/quote]

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Thu May 21, 2009 3:53 pm

JRL wrote:Add
Let>strResponse=
after the Label>Start line

I agree with me_again that you have some logic that I wouldn't use but I think the issue you're reporting is that the variable "strResponse" is set to "YES" and stays that way.

Another logic issue that seems to be working for you but I would not do is multiple repeats using the same variable name.

I also would not use the single letter "x" (or any other single character) as a variable.
Hmm interesting.

I'm very much new to all of this and only just understand what you are saying. With regards to the multiple repeats I take it you are referring to the sections that wait for a bitmap to disappear in FindImagePos? So I should just simply change the variable name so that it is unique each time? Just so that I can learn, why is that important?

As for the use of single letter "x" can I ask why you would not use this and what you would suggest I use instead?

Thanks for your help it is very much appreciated.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu May 21, 2009 4:13 pm

JRL wrote:Add
Let>strResponse=
after the Label>Start line

I agree with me_again that you have some logic that I wouldn't use but I think the issue you're reporting is that the variable "strResponse" is set to "YES" and stays that way.

Another logic issue that seems to be working for you but I would not do is multiple repeats using the same variable name.

I also would not use the single letter "x" (or any other single character) as a variable.
Good point, I just bypassed the stuff I couldn't test and did see the looping problem that RNIB reported.

I agree about the variables too, x and y have particular meanings so that's a potential confusion right there, and using something descriptive always helps, like loopvar or loopcount.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu May 21, 2009 6:06 pm

RNIB wrote:I'm very much new to all of this and only just understand what you are saying. With regards to the multiple repeats I take it you are referring to the sections that wait for a bitmap to disappear in FindImagePos? So I should just simply change the variable name so that it is unique each time? Just so that I can learn, why is that important?

As for the use of single letter "x" can I ask why you would not use this and what you would suggest I use instead?
The main reason for these recommendations is to keep your scripts structured in a way to help avoid confusion. As your scripting skills improve and your scripts get longer it will become more important to you to use standards. Those standards will probably be learned from reading other posted code and also from fixing your own mistakes. Over time you will find yourself doing things a certain way just because you have always done it that way and it worked.

Using single characters as variables will work. You just have to be careful that you don't use the same character in multiple places where you really wanted to save the multiple values. Also, as me_again just said, the larger your scripts become, the more important it will be to have descriptive variable names that will prompt you to recall their meaning. There are several examples of scripts posted to this forum where the script author could not get the script to work and their problem was resolved by using more descriptive variable names rather than single character names. One other thought, Macro Scheduler will let you use a number as a variable name... unless you have a very good reason to do this and you have a good understanding of what's happening DO NOT use numbers as variables.

Using different variable names for multiple repeat loops is again simply a good habit to get into. Its not that using the same variable name over and over for multiple repeat loops can not be made to work. Its that if you have a complex script with many repeat loops of the same name, things will get confusing, especially if you nest the repeat loops.

RNIB
Macro Veteran
Posts: 198
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Post by RNIB » Fri May 22, 2009 9:22 am

Ahh right, yes that does make sense. Good advice!

Many thanks for taking the time to explain that.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts