Search code examples
autohotkey

how to use the image search autohotkey function to wait for an image to appear on screen


i am new to autohotkey i know very little, i need to make a macro in the company where i work and i need to use autohotkey to make that macro.

What I am wanting to do is this. I need to access a JAVA application that stays on my computer, but this application takes some time to start sometimes it's fast sometimes it's slow I don't know exactly how long it can take to load.

What I need the script to do is wait for an image I captured to appear on screen when the application loads, while that image doesn't look like it waits until it appears on screen.

I researched a lot about autohotkey, and the imagesearch function, but I didn't quite understand how to use it

imagefound := false
time_loopStart := A_TickCount
timeout := 6000
sleep_after_each_imagesearch := 300
CoordMode, Pixel
Loop{
     ImageSearch x,y, A_ScreenWidth, A_ScreenHeight, C:\test\Javalogin.png
     if ErrorLevel = 0{
        imagefound := true
        break
  }
  if(A_TickCount - time_loopSart > timeout){
     break

  }
  sleep %sleep_after_each_imagesearch%
}
MsgBox, I found the picture

this script i found on the internet

I accept suggestions if the script needs to be redone etc.

I just need to make the script wait for the image to appear

if you have someone to help I leave my thanks :)


Solution

  • Here's a similar script I'm using.

    Basically, it looks for the picture and if it finds it, it puts the coordinates where it found it in OutputVarX & OutputVarY. If it doesn't find it, the values are 0.

    Loop,
    {
       ; first two store the found position, next 4 as the search area in pixels, then the file.
       ImageSearch, OutputVarX, OutputVarY, 0, 0, 600, 600, e:\play.bmp
    
       ; "0" means no match
       if (OutputVarX > 0) { 
    
           msgbox, %OutputVarX%, %OutputVarY%
           break
    
       }
    
       sleep, 200 
    } 
    

    Some hints:

    • Make a screenshot of your computer showing the image. Crop that to the size of the element and then save it uncompressed. Looking for the original png file may fail if it is rendered in some way.
    • read the help on ImageSearch on how to allow the image to be a bit different.