Loop ImageSearch Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smail19
Posts: 8
Joined: 05 Sep 2017, 15:32

Loop ImageSearch

Post by smail19 » 05 Sep 2017, 15:57

Hi guys . I need your help. I'm trying to do a picture search . i.e
The script searches for one picture in a loop until it finds it. and after he finds it he will point the cursor at it and click on the image, then go to the search for the next picture
I know that I'm close to unraveling, but I've tried many options and I can not get the desired result
here is the code

Code: Select all

; Generated by AutoGUI 1.4.8a
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Loop
{ 
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test1.bmp
if ErrorLevel = 0
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
	break
	sleep, 1000
}
	Loop
{ 	
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test2.bmp
if ErrorLevel = 0
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
	break
	sleep, 1000
	
}
p::Pause
Esc:: ExitApp

:roll:

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Loop ImageSearch

Post by boiler » 05 Sep 2017, 22:30

One issue is that you have several lines after your if statements that you appear to want to be executed if that condition is met. However, the only way you have grouped them is by indenting, which does nothing except make it more readable (and in this case tricks you into thinking they are grouped when they are not). You need to create a code block be preceding those lines with "{" and ending with "}" just like you have done for your loops.

You should also make sure that your CoordMode for both Pixel and Mouse are set to the same -- probably "Screen" since you are searching the entire area of your screen.

smail19
Posts: 8
Joined: 05 Sep 2017, 15:32

Re: Loop ImageSearch

Post by smail19 » 05 Sep 2017, 23:19

boiler wrote:One issue is that you have several lines after your if statements that you appear to want to be executed if that condition is met. However, the only way you have grouped them is by indenting, which does nothing except make it more readable (and in this case tricks you into thinking they are grouped when they are not). You need to create a code block be preceding those lines with "{" and ending with "}" just like you have done for your loops.

You should also make sure that your CoordMode for both Pixel and Mouse are set to the same -- probably "Screen" since you are searching the entire area of your screen.
Hey .Thanks for answering.
These are all the lines they work on, find the image and do all the operations. I just do not know how to do a loop with the search for the first image.i.e . The first cycle should wait until it finds the first picture and click on it. then go to the second picture search
something like this should happen. just do not want to use it many times goto,

Code: Select all

; Generated by AutoGUI 1.4.8a
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
start: 
Loop
{ 
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test1.bmp
If ErrorLevel = 0
{
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
    	goto, second	
}	
else 
{
	goto, start
}		
}
	
second:
Loop
sleep, 500	
{ 
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test2.bmp
if ErrorLevel = 0
{ 
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
	goto, the_third
}
}
the_third:
return

p::Pause
Esc:: ExitApp

the script will be large,and I do not know how to get around goto,

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Loop ImageSearch  Topic is solved

Post by boiler » 05 Sep 2017, 23:27

You had it before with "break". Why did you change that? I would prefer using "until" but what you had would work once you put braces in to mark the code blocks.

smail19
Posts: 8
Joined: 05 Sep 2017, 15:32

Re: Loop ImageSearch

Post by smail19 » 07 Sep 2017, 13:14

boiler wrote:You had it before with "break". Why did you change that? I would prefer using "until" but what you had would work once you put braces in to mark the code blocks.
after two days of searching, I found what I need. thanks to you and your advice!

Code: Select all

; Generated by AutoGUI 1.4.8a
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Loop
	{
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test1.bmp 
		Sleep, 100
	} until !ErrorLevel
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
	
Loop
	{
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test2.bmp 
		Sleep, 100
	} until !ErrorLevel
	MouseMove, %foundX%, %foundY%, 30
	MouseClick, left,%foundX%, %foundY%,
	
}
p::Pause
Esc:: ExitApp
The first picture of the picture is waiting for the first imagination to appear. and then click. then proceeds to search for the second picture
it works ! )) I think you had it in mind when the skali used until ?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Loop ImageSearch

Post by boiler » 07 Sep 2017, 15:45

Yes, that works. You could also have done it with the "if" statement like you had, but you just neglected to group the statements following the "if" into a code block using { }. But like I said, using "until" is cleaner code and easier logic to follow in my opinion.

smail19
Posts: 8
Joined: 05 Sep 2017, 15:32

Re: Loop ImageSearch

Post by smail19 » 07 Sep 2017, 15:59

boiler wrote:Yes, that works. You could also have done it with the "if" statement like you had, but you just neglected to group the statements following the "if" into a code block using { }. But like I said, using "until" is cleaner code and easier logic to follow in my opinion.
I'm going to write a big script and in my opinion it's a great option without using every time goto
спасибо вам большое ! тему можно закрывать :thumbup:

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Loop ImageSearch

Post by jeeswg » 07 Sep 2017, 16:10

I thought I'd share these in case they're useful, from:
best utilities + best AutoHotkey scripts (+ useful tips) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28149

[repeated ImageSearch][ImageSearch: click image every time it's found]
My first project: How hard/easy it is to develop a script for this? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 91#p131491

[repeated PixelSearch][PixelSearch: click pixel every time it's found]
after 4 hours i decided i should get help lol - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 31#p134031
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

davegish
Posts: 1
Joined: 24 Sep 2022, 14:29

Re: Loop ImageSearch

Post by davegish » 24 Sep 2022, 14:37

smail19 wrote:
07 Sep 2017, 13:14
boiler wrote:You had it before with "break". Why did you change that? I would prefer using "until" but what you had would work once you put braces in to mark the code blocks.
after two days of searching, I found what I need. thanks to you and your advice!

Code: Select all

Loop
	{
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_WorkingDir%\test1.bmp 
		Sleep, 100
	} until !ErrorLevel
	MouseClick, left,%foundX%, %foundY%,
THIS IS THE KEY TO EVERYTHING !!

Should definitely be included as an example in the AHK help page for ImageSearch

Post Reply

Return to “Ask for Help (v1)”