Search for either image, click on the one found Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Kris
Posts: 2
Joined: 01 Jan 2020, 13:02

Search for either image, click on the one found

10 Apr 2020, 08:39

Hello, i am trying to learn to write scripts. My script is not working, it does not click on the specified image, but it still clicks close to the image but just not the right one.

Basically i want it to search for 2 images, and double click on the one that it finds, it cannot find both its either 1 or the other, but it doesnt work, it doesnt click on either image even if it is present, it clicks somewhere close to it which is wrong.

this is what i have:

Loop
{
Sleep 4000
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight,C:\cygnusimage1.png
if (ErrorLevel = 0)
Click, %x%, %y%
Click, %x%, %y%
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight,C:\cygnusimage2.png
if (ErrorLevel = 0)
Click, %x%, %y%
Click, %x%, %y%
}

Esc::ExitApp

Please help me fix this!!!

Thank you !
User avatar
Scr1pter
Posts: 1200
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Search for either image, click on the one found

10 Apr 2020, 08:52

Hi and welcome.

Are you sure it does not click on the right image?
ImageSearch's x and y is usually point 0 and 0 of the image (left upper corner).

It may look wrong to you, but it isn't.
You can adjust your click code line:
Click, %x%+10, %y%+10

Also, I do believe your second click line is independent of your if statement.
Use some { } when working with several lines.

Code: Select all

if (ErrorLevel = 0)
{
  Click, %x%+10, %y%+10
  Sleep, 20
  Click, %x%+10, %y%+10
}
P.S. untested code!

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00
User avatar
boiler
Posts: 5910
Joined: 21 Dec 2014, 02:44

Re: Search for either image, click on the one found  Topic is solved

10 Apr 2020, 08:57

One issue is that your second Click statement is getting executed whether the image is found or not because only the line immediately following the if statement is getting conditionally executed. And since x and y have no values when the image isn’t found, it’s just clicking at the current mouse location. If you want to make more than one command subject to the condition, you need to define a code block using braces like you have for the loop. In the modifications I made below, there is no code block defined because I kept it to one Click statement by telling it to click twice.

You should also make sure that CoordMode is set correctly for both Mouse and Pixel.

Code: Select all

CoordMode, Mouse, Screen
CoordMode, Pixel, Screen

Loop
{
	Sleep 4000
	ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight,C:\cygnusimage1.png
	if (ErrorLevel = 0)
		 Click, %x%, %y%, 2
	ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight,C:\cygnusimage2.png
	if (ErrorLevel = 0)
		 Click, %x%, %y%, 2
}

Esc::ExitApp
User avatar
boiler
Posts: 5910
Joined: 21 Dec 2014, 02:44

Re: Search for either image, click on the one found

10 Apr 2020, 09:06

Scr1pter wrote:
10 Apr 2020, 08:52
You can adjust your click code line:
Click, %x%+10, %y%+10
The above doesn’t work with the click statement (actually, it wouldn’t work if it accepted expressions either because then you wouldn’t use the % signs). With Click, you can force its whole single parameter to be an expression, and it turns out to look not very intuitive:

Code: Select all

Click, % x + 10 ", " y + 10 ; could just use a space instead of comma as separator
Kris
Posts: 2
Joined: 01 Jan 2020, 13:02

Re: Search for either image, click on the one found

10 Apr 2020, 09:07

Thank you guys so much, it works now, it clicks on the right image!!!

i will save this page, its great reference!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, JoeWinograd, Karrsen, Kolo, loek6000, mikeyww and 32 guests