My script works – no idea why: ImageSearch Errorlevels

Get help with using AutoHotkey and its commands and hotkeys
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 17:57

Howdy all:

I am so excited I got my script to work but I'm totally flummoxed as to why. This is the intention: search for a cute little image. If found, Double-click. If not found, search for the highlighted version of the cute little image – same thing, lighter colors. If found, double-click. Note that I am using the awesome Findclick library (see here https://www.autohotkey.com/boards/viewtopic.php?f=6&t=18719&p=320713#p320713 ) Because it makes image searching a snap.

This is the code:

Code: Select all

Mbutton::

FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2") 	; Search for the image named autohotkey_travel, click it twice with 50 ms sleeps in between the clicks.
if ErrorLevel=0 		; Now, this line and the next make it so the cute image still gets clicked if it's highlighted. The highlighted state makes the image a couple shades lighter. Autohotkey_travel_light  is the highlighted variant of the same picture. I just created
	{
	FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")	
	}
What puzzles me is that I was expecting it to work with error level 1, which is the code for image not found. If autohotkey_travel is not found (errorlevel =1) then search for autohotkey_travel_light.) But it will not click on autohotkey_travel_light unless the error level is set to 0.

(Don't get me wrong – I'm glad autohotkey_travel_light gets clicked on when the error level is zero, but it makes me curious.)

I feel like I'm talking around in circles. To keep it simple, what is the script doing when I use Errorlevel = 0? What is the script doing when I set the error level to 1?

Thank you.
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 18:24

It’s likely because you allowed for a variation of 40, so while searching for the main image, the light image fell within that variation and was found.
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

Re: My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 18:49

boiler wrote:
03 Apr 2020, 18:24
It’s likely because you allowed for a variation of 40, so while searching for the main image, the light image fell within that variation and was found.
Just for fun, I cranked up the variation. And I took out the last few lines, making this the entire script:

Code: Select all

#Include FindClick.ahk
Mbutton::

FindClick("autohotkey_travel", "Sleep50 oTransBlack,97 n2")
It does not find the highlighted copy.
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 19:24

Then it isn’t finding it, but it’s not apparent from the FindClick documentation that it sets ErrorLevel the way ImageSearch does. To see if it found the image, check the return value of the function.
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 19:28

This seems like how you should set up your code to search for the alternate image if the primary one is not found:

Code: Select all

Mbutton::
	if !FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
		FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
return
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

Re: My script works – no idea why: ImageSearch Errorlevels

03 Apr 2020, 20:38

boiler wrote:
03 Apr 2020, 19:28
This seems like how you should set up your code to search for the alternate image if the primary one is not found:

Code: Select all

Mbutton::
	if !FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
		FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
return
Hey, thank you so much. Is there a way to say, if you don't find autohotkey_travel and you don't find autohotkey_travel_light, look for a third image? I really appreciate your time.
Just for the sake of thinking about it, I don't have another image ready so in place of the third image is going to be a tooltip:

Code: Select all

if !FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
		!FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
tooltip Image could not be found on the screen.
Return
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

04 Apr 2020, 02:52

Close. You need another if:

Code: Select all

 if !FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
	if !FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
	 	 tooltip Image could not be found on the screen.
Return
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

Re: My script works – no idea why: ImageSearch Errorlevels

04 Apr 2020, 09:21

boiler wrote:
04 Apr 2020, 02:52
Close. You need another if:

Code: Select all

 if !FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
	if !FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
	 	 tooltip Image could not be found on the screen.
Return
Got it. So many things to remember!

So now we have a tooltip that displays if the program cannot find either images. Is there a way to get it to execute code, maybe send some other keys, do a Gosub, or display a tooltip that says, "Success!" in the event that it does find the images?

Thank you, again.
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

04 Apr 2020, 10:04

Code: Select all

Found1 := FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
if !Found1
	 Found2 := FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
if (Found1 || Found2)
	ToolTip, Success!
else
	ToolTip, Image could not be found on the screen.
return
You can put any command(s) in place of the tooltips. If you put multiple commands, you need to put them inside a code block:

Code: Select all

Found1 := FindClick("autohotkey_travel", "Sleep50 oTransBlack,40 n2")
if !Found1
	 Found2 := FindClick("autohotkey_travel_light", "Sleep50 oTransBlack,40 n2")
if (Found1 || Found2)
{
	ToolTip, Success!
	SetTimer, ToolTipOff, -1000 ; will execute ToolTipOff routine once after 1 second has elapsed
}
else
{
	ToolTip, Image could not be found on the screen.
	SetTimer, ToolTipOff, -1000
}
return

ToolTipOff:
	ToolTip
return
Last edited by boiler on 04 Apr 2020, 22:16, edited 1 time in total.
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

Re: My script works – no idea why: ImageSearch Errorlevels

04 Apr 2020, 21:55

Thank you. Unfortunately, your beautiful code returned an error at line 13:
https://imgur.com/a/bjeicdt
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

04 Apr 2020, 22:14

I see there was a space between the : and the =. Please remove it. I edited my earlier post.
SmithyZoomZoom
Posts: 36
Joined: 23 Aug 2019, 23:32

Re: My script works – no idea why: ImageSearch Errorlevels

06 Apr 2020, 21:32

Flawless. As you read my complement to your script, be aware that because I employ scripting to overcome a disability, each and every reply you wrote made life just a little bit easier. I'm considering coming up with a skills test to practice what you've shown me but with everything going on in the world it could take me a week or two. Is there a way I could tag you, boiler, so you can see what I come up with?
User avatar
boiler
Posts: 5223
Joined: 21 Dec 2014, 02:44

Re: My script works – no idea why: ImageSearch Errorlevels

06 Apr 2020, 21:49

I am glad to hear you are able to use AHK to help you in this way. Kudos to you for that. I would be happy to check out what you come up with for your skills test.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Baby Flurry Heart, Bing [Bot], Bruce27, Clemens375, Google [Bot], himanshu, Rohwedder, songdg and 147 guests