Page 1 of 1

Search for either image, click on the one found

Posted: 10 Apr 2020, 08:39
by Kris
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 !

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

Posted: 10 Apr 2020, 08:52
by Scr1pter
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!

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

Posted: 10 Apr 2020, 08:57
by boiler
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

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

Posted: 10 Apr 2020, 09:06
by boiler
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

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

Posted: 10 Apr 2020, 09:07
by Kris
Thank you guys so much, it works now, it clicks on the right image!!!

i will save this page, its great reference!