Repeat function until image cant be found

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
Kapples
Newbie
Posts: 3
Joined: Tue Dec 19, 2017 1:16 pm

Repeat function until image cant be found

Post by Kapples » Tue Dec 19, 2017 1:33 pm

Hi
I was just wondering how to make function repeating itself ( mouse clicking ) until an certain image(s) cant be found on screen/certain window.
Im all new to this, but I couldnt think of ways how to do this. Help? :D
Sorry for my english btw

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

Re: Repeat function until image cant be found

Post by Marcus Tettmar » Wed Dec 20, 2017 10:09 am

You would want to use a FindImagePos statement inside a loop and loop until it returns no matches. Something like:

Code: Select all

FindImagePos>%BMP_DIR%\needle.bmp,SCREEN,0.7,1,X,Y,NumFound,CCOEFF
While>NumFound>0
  .. 
  .. do whatever 
  ..
  FindImagePos>%BMP_DIR%\needle.bmp,SCREEN,0.7,1,X,Y,NumFound,CCOEFF
EndWhile
This While loop will keep looping until no match is found (until NumFound=0)
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

Kapples
Newbie
Posts: 3
Joined: Tue Dec 19, 2017 1:16 pm

Re: Repeat function until image cant be found

Post by Kapples » Sun Dec 24, 2017 12:47 am

okey... so so far it works, only sometimes even when image has disapeared from screen it still keeps clicking.... my guess is that its cause my "needle.bmp" is small, like 12 pixels and probably on screen script catches these pixels somewhere else. I was wondering can I set an window like rectangle in which script would search my "needle.bmp"?

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

Re: Repeat function until image cant be found

Post by Marcus Tettmar » Tue Dec 26, 2017 10:26 pm

Yes, please see the manual for FindImagePos. You can specify a window, or even capture a portion of the screen with ScreenCapture and then pass that to 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?

Kapples
Newbie
Posts: 3
Joined: Tue Dec 19, 2017 1:16 pm

Re: Repeat function until image cant be found

Post by Kapples » Thu Dec 28, 2017 1:12 am

And... how can I set an function repeating itself until an certain image doesnt appear? :mrgreen:

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

Re: Repeat function until image cant be found

Post by JRL » Thu Dec 28, 2017 5:56 am

Here's a sample.

This writes and runs a script that displays a Macro Scheduler dialog. It also exports a 7 x 6 pixel bmp file that is a portion of the robot head icon in the dialog. The dialog position is acquired then, using that position, a 50 x 50 pixel area where the robot head is located is captured over and over again in a loop. The 50 x 50 pixel captured area is compared to the 7 x 6 pixel bmp file. When the dialog closes, the captured area no longer has the robot head, so Findimagepos> no longer finds the 7 x 6 pixel image in the captured image and the dialog completes. Also added a counter so you can see how many times the Loop cycled.

This works on my Window 10 computer. Your results may fail and you might have to recapture and re-import the bot.bmp file.

Code: Select all

Message>
ExportData>BOT.BMP_DATA,%temp_dir%bot.bmp
ExportData>JRLSCRIPT.SCP_DATA,%temp_dir%JRLScript.scp
Let>RP_Wait=0
RunProgram>%command_line% %temp_dir%JRLScript.scp
WaitWindowOpen>CustomDialog
Wait>3
GetWindowPos>CustomDialog,WinX,WinY
Let>WinW=%WinX%+50
Let>WinH=%WinY%+50
SetFocus>CustomDialog
Let>kk=0
Label>Loop
Add>kk,1
Wait>0.01
ScreenCapture>WinX,WinY,WinW,WinH,%temp_dir%JRLCap.bmp
FindImagePos>%temp_dir%bot.bmp,%temp_dir%JRLCap.bmp,0.7,1,X,Y,NumFound,CCOEFF
If>NumFound=1
  SetControlText>Macro Scheduler Message,TMemo,1,%kk%%crlf%%NumFound%
  Goto>Loop
EndIf
MDL>Done
/*
JRLSCRIPT.SCP_DATA:
FFFE4400690061006C006F0067003E004400690061006C006F00670031000D000A006F0062006A0065006300740020004400690061006C006F00670031003A002000540046006F0072006D000D000A0065006E0064000D000A0045006E0064004400690061006C006F0067003E004400690061006C006F00670031000D000A0
0530068006F0077003E004400690061006C006F00670031000D000A0057006100690074003E00310030000D000A00
*/
/*
BOT.BMP_DATA:
424DDE00000000000000360000002800000007000000060000000100200000000000A80000000000000000000000000000000000000021688EFF00AFFFFF0A7DBFFF0299F0FF00A6FFFF009FFFFF00A1FFFF079DE2FF116B95FF146583FF12648BFF03A4EDFF00AEFFFF00ACFFFF107A9FFF079DD4FF05A9DFFF0896C8FF107
397FF00BEFFFF02B0F5FF0A93BAFF05A8D8FF146178FF04B2E3FF107B98FF00CBFFFF00BFF7FF0D839BFF03BDE3FF08BEE0FF09A9C8FF184953FF0B92ABFF03C7F2FF39D3E3FF2B828DFF258996FF25494DFF31828EFF30858EFF2B838EFF
*/

User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Re: Repeat function until image cant be found

Post by uniadv » Sat Aug 15, 2020 11:00 am

Marcus Tettmar wrote:
Wed Dec 20, 2017 10:09 am
You would want to use a FindImagePos statement inside a loop and loop until it returns no matches. Something like:

Code: Select all

FindImagePos>%BMP_DIR%\needle.bmp,SCREEN,0.7,1,X,Y,NumFound,CCOEFF
While>NumFound>0
  .. 
  .. do whatever 
  ..
  FindImagePos>%BMP_DIR%\needle.bmp,SCREEN,0.7,1,X,Y,NumFound,CCOEFF
EndWhile
This While loop will keep looping until no match is found (until NumFound=0)
This is what i'm looking for. Thanks :D

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