Trouble creating If Statements involving Image Recognition
Moderators: Dorian (MJT support), JRL
Trouble creating If Statements involving Image Recognition
Hi All,
I'm pretty new to with this software and love it, but I'm having difficulty with something that is probably very simple to fix.
Basically, I'm having a lot of difficulty creating If Statements if the If Statement involves using Image Recognition or Image Capture.
I'm trying to add records to a web database for my job.
I'm an End-User, so I do not have administrative access to the site.
The site uses a lot of javascript, so mouseovers, drop downs, and scrollable embedded tables where the records change when you click javascripted next page arrows, etc.
The images that I have captured for the purposes of the macro DO NOT change when the embedded tables change.
Basically, I've written a macro that should:
a) Do a CTRL-F to open the Find function in IE8 (work computer, so I can't change the browser I use)
b) Copy and paste a Name from a particular Excel file into the Find field in IE.
c) Check the Find result to the right of the field "No matches found" or "1 match" (that will be the only two possible results of the Find).
d) If the Find (LCTRL-F) returns "No matches found" then the macro should click on the next page arrow on the embedded table (captured image),
e) When the table is ready (a simple Wait command takes care of this) the macro should repeat steps c and d until the result in Find field in IE is "1 match".
For some reason, the Macro just stalls after completing step d the first time. No error, no pop-up window or text box, no timeout, it just keeps running, but does nothing.
I've tried to include a SetFocus command in the loop to re-focus MacroScheduler on the webpage. I've tried to include Waits, and mouse clicks to make sure the program was focused on the correct page, nothing works.
Can someone please just put up some examples of how to structure an If/EndIf, a Repeat/Until, and If/Else, and a Loop command that relies on Image Recognition to satisfy the statement?
I will post my code as well in a following message. This is urgent, so any helpd would be appreciated!
Thanks!
rjw524
I'm pretty new to with this software and love it, but I'm having difficulty with something that is probably very simple to fix.
Basically, I'm having a lot of difficulty creating If Statements if the If Statement involves using Image Recognition or Image Capture.
I'm trying to add records to a web database for my job.
I'm an End-User, so I do not have administrative access to the site.
The site uses a lot of javascript, so mouseovers, drop downs, and scrollable embedded tables where the records change when you click javascripted next page arrows, etc.
The images that I have captured for the purposes of the macro DO NOT change when the embedded tables change.
Basically, I've written a macro that should:
a) Do a CTRL-F to open the Find function in IE8 (work computer, so I can't change the browser I use)
b) Copy and paste a Name from a particular Excel file into the Find field in IE.
c) Check the Find result to the right of the field "No matches found" or "1 match" (that will be the only two possible results of the Find).
d) If the Find (LCTRL-F) returns "No matches found" then the macro should click on the next page arrow on the embedded table (captured image),
e) When the table is ready (a simple Wait command takes care of this) the macro should repeat steps c and d until the result in Find field in IE is "1 match".
For some reason, the Macro just stalls after completing step d the first time. No error, no pop-up window or text box, no timeout, it just keeps running, but does nothing.
I've tried to include a SetFocus command in the loop to re-focus MacroScheduler on the webpage. I've tried to include Waits, and mouse clicks to make sure the program was focused on the correct page, nothing works.
Can someone please just put up some examples of how to structure an If/EndIf, a Repeat/Until, and If/Else, and a Loop command that relies on Image Recognition to satisfy the statement?
I will post my code as well in a following message. This is urgent, so any helpd would be appreciated!
Thanks!
rjw524
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Most examples on these forums use an If/Endif statement, so a quick search should reveal the answer to that. In fact the Image Recognition Wizard outputs an If/Endif for you. Like this:
This will of course click on the first occurence and really assumes there will be only one, which is the usual scenario. If you wanted an Else:
If you had a situation were there were multiple matches and you want to loop through all of them you could use a Repeat/Until like this:
A loop where the condition is that the image exists could look something like this:
If it should keep looping while it does NOT exist then change the While condition to NumFound=0. But then you might as well use the WaitScreenImage command.
Hope this helps.
Code: Select all
Let>FIP_SCANPIXELS=ALL
FindImagePos>%SCRIPT_DIR%\Blob_1.bmp,SCREEN,0,1,XArr,YArr,NumFound
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Endif
Code: Select all
Let>FIP_SCANPIXELS=ALL
FindImagePos>%SCRIPT_DIR%\Blob_1.bmp,SCREEN,0,1,XArr,YArr,NumFound
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Else
//didn't find it!
Endif
Code: Select all
FindImagePos>%SCRIPT_DIR%\Blob_1.bmp,SCREEN,0,1,XArr,YArr,NumFound
//click on ALL matches in turn
If>NumFound>0
Let>k=0
Repeat>k
Let>k=k+1
//click on it
MouseMove>XArr_%k%,YArr_%k%
LCLick
Wait>0.5
Until>k=NumFound
Code: Select all
FindImagePos>%SCRIPT_DIR%\Blob_1.bmp,SCREEN,0,1,XArr,YArr,NumFound
While>NumFound>0
//do something ...
//and keep checking ...
FindImagePos>%SCRIPT_DIR%\Blob_1.bmp,SCREEN,0,1,XArr,YArr,NumFound
//don't forget a small wait to help stop things locking up
Wait>0.05
EndWhile
Hope this helps.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
This is a great start.
Thanks, Marcus.
I sent you a PM.
I sent you a PM.
ending loop if image not found
hello Marcus Tettmar i have been using one of the codes you have provided above with a little twik the script loops as intended however i would like the loop to stop if the image is not found on the page.
eg of the code
SetFocus>Windows Internet Explorer*
wait>1
FindImagePos>%SCRIPT_DIR%\five berriers.bmp,SCREEN,0,1,XArr,YArr,NumFound
If>NumFound>0
Let>k=1
Repeat>k
Let>k=k+1
Wait>0.5
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\Users\Public\Dropbox\getjam images\Screencapture.bmp
FindImagePos>C:\Users\Public\Dropbox\getjam images\five berriers.bmp,C:\Users\Public\Dropbox\getjam images\Screencapture.bmp,5,1,XPos,YPos,imagefound
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
EndIf
EndIf
Until>k=NumFound
the code above repeats continuously is it possible to stop the loop if the image FindImagePos>%SCRIPT_DIR%\five berriers.bmp,SCREEN,0,1,XArr,YArr,NumFound is not present on the page.
thank you
eg of the code
SetFocus>Windows Internet Explorer*
wait>1
FindImagePos>%SCRIPT_DIR%\five berriers.bmp,SCREEN,0,1,XArr,YArr,NumFound
If>NumFound>0
Let>k=1
Repeat>k
Let>k=k+1
Wait>0.5
GetScreenRes>sX,sY
ScreenCapture>0,0,sX,sY,C:\Users\Public\Dropbox\getjam images\Screencapture.bmp
FindImagePos>C:\Users\Public\Dropbox\getjam images\five berriers.bmp,C:\Users\Public\Dropbox\getjam images\Screencapture.bmp,5,1,XPos,YPos,imagefound
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
EndIf
EndIf
Until>k=NumFound
the code above repeats continuously is it possible to stop the loop if the image FindImagePos>%SCRIPT_DIR%\five berriers.bmp,SCREEN,0,1,XArr,YArr,NumFound is not present on the page.
thank you
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Yes, just add an Else clause and have it exit if not found:
Code: Select all
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
Else
//Exit the script
Exit>0
EndIf
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
ending loop if image not found
is there another option of exiting the loop but continuing with the rest of the script.
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
Else
//Exit the script
Exit>0
EndIf
that works but it exits the whole script what i would like to do is exit the loop and continue with the rest of the script if possible.
thank you again
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
Else
//Exit the script
Exit>0
EndIf
that works but it exits the whole script what i would like to do is exit the loop and continue with the rest of the script if possible.
thank you again
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Yes, either of these should do it:
Or
Code: Select all
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
Else
//Exit the loop
Let>k=NumFound
EndIf
Code: Select all
If>imagefound>0
wait>0.2
MouseMove>XPos_0,YPos_0
LClick
Else
//Exit the loop
Goto>afterloop
EndIf
..
..
Until>k=NumFound
Label>afterloop
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Re: Trouble creating If Statements involving Image Recogniti
I am trying to follow the above instructions but I am doing something wrong, as I keep getting a message say "Invalid Numeric Value for MouseMove command"
I'm sure its something simple, but I can't figure it out.. so alas after 4 days I am seeking assistance.
Here is my exact code:
I'm trying to look for the "Join" image. (numfound1), which once clicked pulls down a menu that will have the next 2 images (checkbox and enter)
If the Join Image is found ... click the checkbox image and then the enter image.
When there aren't anymore "join" images, move onto the rest of the script.
Could someone steer me in the proper direction? I've searched that specific error code and Im not sure how to change the mouse move to make it correct. I used the above example... but its not working
Thank you in advance for any assistance you can give this humble beginner!
I'm sure its something simple, but I can't figure it out.. so alas after 4 days I am seeking assistance.
Here is my exact code:
Code: Select all
//Find and Left Click Center of
FindImagePos>C:\Users\Win7\Macro Scheduler 14\IMAGES\Join.bmp,SCREEN,0,1,XArr,YArr,NumFound1
//click on ALL matches in turn
If>NumFound1>0
Let>k=0
Repeat>k
Let>k=k+1
//click on it
MouseMove>XArr_%k%,YArr_%k%
LCLick
Wait>0.5
Else
//Exit the loop
Goto>afterloop
EndIf
Wait>5.0
//Find and Left Click Center of
FindImagePos>C:\Users\Win7\Macro Scheduler 14\IMAGES\checkbox.bmp ,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
Wait>0.5
If>NumFound>0
Wait>0.5
MouseMove>XArr_0,YArr_0
Wait>0.5
LClick
Wait>0.5
Endif
Wait>5.0
//Find and Left Click Center of
FindImagePos>C:\Users\Win7\Macro Scheduler 14\IMAGES\enter.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
Wait>0.5
If>NumFound>0
Wait>0.5
MouseMove>XArr_0,YArr_0
Wait>0.5
LClick
Wait>0.5
Endif
Wait>5.0
Until>k=NumFound1
Wait>5.0
Label>afterloop
I'm trying to look for the "Join" image. (numfound1), which once clicked pulls down a menu that will have the next 2 images (checkbox and enter)
If the Join Image is found ... click the checkbox image and then the enter image.
When there aren't anymore "join" images, move onto the rest of the script.
Could someone steer me in the proper direction? I've searched that specific error code and Im not sure how to change the mouse move to make it correct. I used the above example... but its not working
Thank you in advance for any assistance you can give this humble beginner!
Re: Trouble creating If Statements involving Image Recogniti
Just a guess but if your first FindImagePos> finds one image, the one image will be at position:
XArr_0,YArr_0 where XArr_0 has a value and YArr_0 has a value
You are setting k to the value 1 at the start of your Repeat> loop so you are looking at position
XArr_1,YArr_1. If you only have one image XArr_1 has no integer value and YArr_1 has no integer value
Try setting k to -1 and see if that helps.
...If>NumFound1>0
Let>k=-1
Repeat>k
Let>k=k+1
//click on it
MouseMove>XArr_%k%,YArr_%k%
LCLick...
XArr_0,YArr_0 where XArr_0 has a value and YArr_0 has a value
You are setting k to the value 1 at the start of your Repeat> loop so you are looking at position
XArr_1,YArr_1. If you only have one image XArr_1 has no integer value and YArr_1 has no integer value
Try setting k to -1 and see if that helps.
...If>NumFound1>0
Let>k=-1
Repeat>k
Let>k=k+1
//click on it
MouseMove>XArr_%k%,YArr_%k%
LCLick...
Re: Trouble creating If Statements involving Image Recogniti
IT WORKED! IT WORKED!! And thank you for explaining it as well as your suggested code. I think I now understand.. I think.JRL wrote: ...If>NumFound1>0
Let>k=-1
Repeat>k
Let>k=k+1
//click on it
MouseMove>XArr_%k%,YArr_%k%
LCLick...
EDITED... I spoke too soon. It found the first "join" and then continued to click the next 2 (checkbox and enter).. However I restarted it to do it again and continue with my editing and since now there is only 1 "join" ... its passing it right up????
EDITED AGAIN: My bad! The 2nd "join" image is slightly different. so I'll work on this.
SOLUTION: I watched this video http://help.mjtnet.com/article/39-how-t ... -functions and then changed
THIS
SCREEN,0,1,XArr,YArr,NumFound1
TO THIS
SCREEN,0.6,1,XArr,YArr,NumFound1,CCOEFF
Using:
MS 14.2.07 (regular)
Windows 7
Browser: Chrome
MS 14.2.07 (regular)
Windows 7
Browser: Chrome