Passing a file path to ImageSearch as a variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest24

Passing a file path to ImageSearch as a variable

23 Mar 2018, 12:02

I'm trying to pass the name of an image (image.png) to ImageSearch as a variable.

Code: Select all

imageName := "image.png"
f::
	ImageSearch, posX, posY, 0, 0, A_ScreenWidth, A_ScreenHeight, %imageName% ;  <<< variable
	If ErrorLevel = 2
		{
		MsgBox, 2
		}
	Else If ErrorLevel = 1
		{
		MsgBox, 1
		}
	Else If ErrorLevel = 0
		MsgBox, 0

return

This code gives me only Errorlevel = 2 as a result. Does ImageSearch simply not accept this syntax?

I tried a few variations on this to double check spelling and correct directory, for example:

Code: Select all

f::
	ImageSearch, posX, posY, 0, 0, A_ScreenWidth, A_ScreenHeight, image.png ; <<< actual file name
	If ErrorLevel = 2
		{
		MsgBox, 2
		}
	Else If ErrorLevel = 1
		{
		MsgBox, 1
		}
	Else If ErrorLevel = 0
		MsgBox, 0

return
This works fine, and the directory is exactly the same. I'm using SetWorkingDir %A_ScriptDir%, and the script and image are in the same folder.

Any help greatly appreciated! Thank you!
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Passing a file path to ImageSearch as a variable

23 Mar 2018, 16:53

Lack of CoordMode, Pixel, Screen in the script will cause the issue if a window is active and you are searching out of bounds. (fullscreen in your case)

Give this a try and see the results:

Code: Select all

#NoEnv
SetWorkingDir, %A_ScriptDir%
CoordMode, Pixel, Screen
imageName := "image.png"

f::
	ImageSearch, posX, posY, 0, 0, A_ScreenWidth, A_ScreenHeight, %imageName% ;  <<< variable
    MsgBox % imageName . "`nErrorlevel = " . ErrorLevel
return
HTH

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750 and 379 guests