Trying to do a image search with V2 scripts - keep getting error

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
mikeseo
Posts: 3
Joined: 03 May 2024, 14:08

Trying to do a image search with V2 scripts - keep getting error

03 May 2024, 14:26

Code: Select all

#Requires AutoHotkey v2.0

    ; Define the area for the image search
    left := 0
    top := 0
    right := 1000
    bottom := 600
    imagePath := "img.png"  ; Adjust the path to where your image is stored

    ; Perform the image search within the specified area
    ImageSearch &OutputVarX, &OutputVarY, left, top, right, bottom, img.png

    ; Check if the image was found
    if (foundX != "")
    {
        ; Image found, click on the coordinates
        MouseClick "left", &OutputVarX, &OutputVarY
        MsgBox "Image found and clicked "
    }
    else
    {
        ; Image not found
        MsgBox "Image not found."
    }
return
[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]

I have the above code, the img.png is the same folder as the ahk file.
But when I run it, the AHK app keeps on showing error message

Warning: This variable appears to never be assigned a value.
▶ 011: ImageSearch(&OutputVarX, &OutputVarY, left, top, right, bottom, img.png)


What could be wrong?

Here is a recording of the error in video
https://vento.so/view/a5bfb0a8-3f94-42c3-bb04-0858598c9e81?utm_medium=share
gregster
Posts: 9079
Joined: 30 Sep 2013, 06:48

Re: Trying to do a image search with V2 scripts - keep getting error

03 May 2024, 14:50

As a string, the file name should be quoted.
Compare https://www.autohotkey.com/docs/v2/lib/ImageSearch.htm#ExBasic

Or simply use the variable imagePath which you defined earlier (unquoted).
mikeseo
Posts: 3
Joined: 03 May 2024, 14:08

Re: Trying to do a image search with V2 scripts - keep getting error

03 May 2024, 23:44

Thanks for the reply
Do I have to focus the window first?
User avatar
boiler
Posts: 17179
Joined: 21 Dec 2014, 02:44

Re: Trying to do a image search with V2 scripts - keep getting error

04 May 2024, 02:17

If your search rectangle is based on a particular window, then yes. If they’re relative to the screen, then the window doesn’t need focus, but it must be visible. And you would need to set the CoordMode:

Code: Select all

CoordMode 'Pixel', 'Screen'
mikeseo
Posts: 3
Joined: 03 May 2024, 14:08

Re: Trying to do a image search with V2 scripts - keep getting error

04 May 2024, 04:06

Does the photo need to be a png?

I followed @gregster link and copied to the following:

Code: Select all

CoordMode "Pixel"  ; Interprets the coordinates below as relative to the screen rather than the active window's client area.
try
{
    if ImageSearch(&FoundX, &FoundY, 0, 0, 600, 450, "stop.png")
        MsgBox "The icon was found at " FoundX "x" FoundY
    else
        MsgBox "Icon could not be found on the screen."
}
catch as exc
    MsgBox "Could not conduct the search due to the following error:`n" exc.Message
Below is a screen recording video

https://vento.so/view/1a10729d-f3ba-4ed7-a44d-ff10f3305a7e?utm_medium=share

What could went wrong?

Screen resolution?

The button is not actually an image, it is a text button.

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]
User avatar
boiler
Posts: 17179
Joined: 21 Dec 2014, 02:44

Re: Trying to do a image search with V2 scripts - keep getting error

04 May 2024, 04:51

Definitely use png instead of jpg because jpg is lossy and will not match the image exactly.

It could be that the icon is rendered differently every time it is displayed. Or if you have a Windows scaling factor of other than 100%, then that definitely would cause a problem.

How did you capture your png file? If you post both your stop.png file as an attachment and a screenshot of your desktop as a png, then we can see if they should be able to match or not.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], robinson and 39 guests