If Statement Not Always Executing

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

If Statement Not Always Executing

Post by tommystery » Thu May 22, 2008 2:16 pm

Alright, so after running the script for a while I noticed that my previous problem persisted. I also figured out that Wait>.1 or .2 or .3 etc. does absolutely nothing and the variable has to be a whole integer.

In any case;

Here is a sample of my script:

Label>Loops1
FindImagePos>C:\macrolive\submit.BMP,SCREEN,20,0,XPos,YPos,NumFound
If>NumFound=0
Goto>Loops1
Endif

Here is what executed as per the log file:

5/22/2008 10:11:04:082 - Label>Loops1
5/22/2008 10:11:04:223 - FindImagePos>C:\macrolive\submit.BMP,SCREEN,20,0,XPos,YPos,NumFound
5/22/2008 10:11:04:223 - Endif

EDIT: Ok, it seems like that's what the log displays when the statement is false. So perhaps it is executing. I added a "Message>NumFound" at the end of the script but now I can't replicate the problem to verify that the If statement isn't executing.

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 22, 2008 3:00 pm

Wait>.1 does work. Run this macro, and then delete all Wait> and see the difference.

MDL>Start
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
Wait>.1
MDL>Done

To investigate the loop problem test by adding

MDL>NumFound

after the FindImagePos> line, then it will show you each time the loop executes, and if the image is found.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri May 23, 2008 2:05 am

Hi tommystery,
For the purpose of debugging, add

Label>Loops1
FindImagePos>C:\macrolive\submit.BMP,SCREEN,20,0,XPos,YPos,NumFound
Msg>NumFound
Wait>30
If>NumFound=0
Msg>NumFound
Wait>30
Goto>Loops1
Endif

This should help verify if the If> statement has been executed.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri May 23, 2008 8:02 am

Use the debugger. That's the best way to see what is being executed and when.

http://www.mjtnet.com/blog/2006/05/17/use-the-debugger/
http://www.mjtnet.com/blog/2007/09/12/d ... bug-debug/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

Post by tommystery » Fri May 23, 2008 3:48 pm

AH! Thanks so much. I was just complaining to my gf the other night that this is a great program but doesn't have any debugging tools. Guess I was wrong, I even used the advanced editor option but never noticed it. 8)

And no, the if statement is being executed, I was incorrect in that assumption. The image position is just sometimes returned as something else which is similar, even though I've reduced the colour tolerance to 10.

BTW, could anyone help me with this.

I wish to move the mouse from the found image but I get an invalid value error if I do the following:

FindImagePos>C:\macrolive\cancel.BMP,SCREEN,20,0,XPos,YPos,NumFound Wait>1
XMove=XPos_0+10
YMove=YPos_0+30
MouseMove>XMove,YMove

I even saw a thread about this the other day but I'm not sure where. In either case, I'll probably figure it out before someone responds.

But thanks for pointing out the debugger.

EDIT: Figured it out...

Let>XMove...
Let>YMove...

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri May 23, 2008 3:55 pm

There's an entire topic on the debugger in the help file. When you go into the advanced editor you will see a menu option called "Debug" and there are toolbar buttons for Run, Step and Stop. And on the left is a tab for "Watch List". In the basic editor the tooltip for the Advanced Editor button says "Full Editor, Debugger ...". Maybe v11 will have a massive red button in the center of every window saying "CLICK HERE FOR THE DEBUGGER" :-) Seriously though, I think we need to change the "Advanced Editor" button to say "Debug Editor" or something like that.

Your code should be:

Code: Select all

FindImagePos>C:\macrolive\cancel.BMP,SCREEN,20,0,XPos,YPos,NumFound Wait>1
Let>XMove=XPos_0+10
Let>YMove=YPos_0+30
MouseMove>XMove,YMove
Or you could do:

Code: Select all

FindImagePos>C:\macrolive\cancel.BMP,SCREEN,20,0,XPos,YPos,NumFound Wait>1
MouseMove>{%XPos_0%+10},{%YPos_0%+30}
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

Post by tommystery » Fri May 23, 2008 4:30 pm

Thanks Marcus for your help. I wonder if I posted the edit before or after you posted your reply. :)

Maybe you could help me with this. I've reduced the colour tolerance to zero, ran a continious loop, and every now and then the script will click on the wrong button.

The buttons are identical, with one having the text submit and the other the text cancel. I've also recropped the images and made them larger (hence the offset); I figured that a larger bitmap would reduce these errors however they persist.

Any ideas? I can upload the images of the two, but it's identical to the "Preview" and "Submit" buttons when you're posting on the forum.

tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

Post by tommystery » Fri May 23, 2008 4:33 pm

Haha. I sparked a sticky. :P

tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

Post by tommystery » Fri May 23, 2008 4:47 pm

Ah, I've made my own solution that I am pleased with. 8)

I simply made needle image of the two buttons (they're beside eachother) and offset to the one I want.

I'd also want to leave a tip for other users. In the past week I've worked on my script and effectly gotten rid of all Wait> commands by replacing them with check loops. If you need quick execution this might be the best solution. For educational purposes, here is a copy of my final script.

SetFocus>WindowName
Label>Loop13
MouseMove>162,490
GetCursorPos>XC,YC
If>{(%XC%162) OR (%YC%490)}
Goto>Loop13
Endif
LClick
WaitWindowOpen>WindowName2 {I know that I've been advised against using this line but it works well for me}
SetFocus>WindowName2
Label>Loops7
FindImagePos>C:\macrolive\cancel.BMP,SCREEN,0,0,XPos,YPos,NumFound
If>NumFound=0
Goto>Loops7
Endif
Let>XMove=XPos_0+100
Let>YMove=YPos_0+10
Label>Loop14
MouseMove>XMove,YMove
GetCursorPos>XC,YC
If>{(%XC%%XMove%) OR (%YC%%YMove%)}
Goto>Loop14
Endif
LClick

Finally; I'd like to thank Armsys and Marcus for all of their help.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri May 23, 2008 4:49 pm

Override the value of FIP_SCANPIXELS to make it scan more of the pixels to get a more accurate match. See the help file topic for FindImagePos.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

tommystery
Junior Coder
Posts: 26
Joined: Fri May 16, 2008 12:30 pm

Post by tommystery » Fri May 23, 2008 7:41 pm

Ah, that makes perfect sense now. Can this be defined at the beginning of the script or does it have to be defined each time that FindImagePos is executed?

And roughly how much slower will execution be if this is set to 200 as opposed to 100?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri May 23, 2008 7:48 pm

It can be defined once. It is global - until modified. So unless you want different values for different FindImagePos commands, yes, you can set it once at the top of the script. You're unlikely to notice any performance loss if you change it to 200, or even higher. But experiment.

You could probably also do with making your capture smaller. Include less of the button background so that the caption of the button makes up a larger percentage of the image.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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