ImageSearch. Find (same) images (if they are exist) in certain window. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ARkaine
Posts: 20
Joined: 30 Dec 2019, 07:58

ImageSearch. Find (same) images (if they are exist) in certain window.

Post by ARkaine » 27 Nov 2022, 04:50

ATM I have code, that works fine with one image (button that I need to press) in window,
but I didnt find success on making it work (search) with multiple (same) images (buttons).
I found some codes, that contain "loop" (or maybe required something else), but they didn't work for me.

Here's the code

Code: Select all

CoordMode, Pixel, Window

x::
ImageSearch,ix,iy,0, 0,a_screenWidth,a_screenHeight,%A_ScriptDir%\Test.png
MouseClick, left, ix, iy
But how to make it work with multiple same images?

I need something like:

ImageSearch,ix,iy,0, 0,a_screenWidth,a_screenHeight,%A_ScriptDir%\Test.png
MouseClick, left, ix, iy
ImageSearch,ix,iy,0, 0,a_screenWidth,a_screenHeight,%A_ScriptDir%\Test.png (should the same image file have another name to be found?).
If (next same) image exists, click it and search for next image.
If (next same) image doesn't exist, return.
Etc about 5 times.

What is correct way to loop it?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: ImageSearch. Find (same) images (if they are exist) in certain window.  Topic is solved

Post by mikeyww » 27 Nov 2022, 07:12

One way is below. The search does not require changing the image name with each iteration, if you would like to search for the same image each time.

If the image exists in multiple places at the same time, only one image (always top left) will be targeted per iteration.

Code: Select all

image = %A_ScriptDir%\test.png
If !FileExist(image)
 MsgBox, 48, Error, File not found.`n`n%image%

x::
SoundBeep, 1500
CoordMode, Mouse          ; To search the screen
CoordMode, Pixel
Loop, 5 {                 ; Search up to five times
 Sleep, 25
 ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image%
 If ErrorLevel {          ; If image is not found,
  SoundBeep, 1000
  Return                  ;  then return,
 } Else MouseClick,, x, y ;  otherwise click image's top left corner
}
Return

Post Reply

Return to “Ask for Help (v1)”