Click on whichever image exists

White Keys

Black Keys

So in the app I’m trying to configure (Cubase 15), there is an icon that appears occasionally that I want to create a click on image macro for. However depending on the app’s status it can appear as either a black icon or white icon. Only one of the two icons will ever be present in the front most window. I’m trying to figure out the cleanest way to get KM to click on which ever image happens to be present. Having a little trouble wrapping my head around the logic.

Put the first image into a try block. Set a short timeout. Set fail abort’s macro. Put the second image into a catch block.

1 Like

@vincent_ardern thanks I think this was the very fix. Stupid ChatGPT ain’t gotta nothing on this forum!

1 Like

I suspect that the answer will depend upon what you have in mind by “cleanest”.

In programming, try–catch tends (at least for most programming languages) to be seen as a last resort, or for dealing with unavoidable or unforeseen exceptional situations. So try–catch can be very useful but may not be theoretically pure enough for purists!

I have only glanced at the page so far, but for more on this point you might be interested in a discussion that I found on Stack Exchange.

Try-catch is also not “cleanest” in terms of the log for the Keyboard Maestro Engine, since failure to find an image will result in an error being logged.

If any of that matters to you, @fossie (for philosophical or practical reasons!), you could instead test for the presence of the image using an if action set to test whether the first image is present.

1 Like

...or you can test for both images -- if you use "any of the following are true" then the resulting %FoundImage% will be the rectangle of the first image matched, working down the list:

4 Likes

@Nige_S @kevinb Awesome! Yeah, I was wondering exactly how to use an image as a var (sorry total noob really). Thanks.

And if I use an image as a var I can later reference it without having to run an image search again if I need it again further down the chain in the macro? This might be handy and make things a little smoother, if this is in fact the case? I’m guessing though it could only be used to reference the found image.

Say I run an image search for either image, A or B, like above and it finds image A and saves it as a var. But later on in the macro that image change to state B, I’m guessing I can no longer use the previously saved var to reference (i.e. move mouse to %FoundImage%)

Thanks again

1 Like

I think we all learn new tricks with KM all the time; for instance, @Nige_S’s use of %FoundImage% was not one that had occurred to me.

I think the answer to what you probably have in mind here is “yes”, but let’s revise a couple of terms so that we are quite clear about what is being discussed. The text in bold is my added emphasis.

From the Tokens page of the Wiki:

Tokens in Keyboard Maestro represent placeholders, that you enter during design time, for information that will be replaced in the text at run time.

All tokens return a text value, even if the source data was some other data type.

And from the Variables page:

Variables contain only plain, un-styled, text and may be used in any token text field in an action, as well as in Calculations in a numerical field if they contain a valid number or expression.

So it is not actually possible to “use an image as a var”. If we look at the page for the FoundImage Token, we find that the token returns “the details of the last found image”. These include the image’s dimensions and its co-ordinates on the screen.

Therefore, if we set a variable to the value of %FoundImage%, we are storing that data about the image and its position, and not the image itself.

For your purposes, that’s all good, provided that the images that you are searching for always appear in the same part of the window or screen (as I presume they would..?), since you can use data from your variable for an automated click in the right area, using the move or click mouse action without having to search again for the image.

So…

Once you have stored the data about the area you want to click on in a variable, it no longer matters what is shown in that area. You have already obtained the screen co-ordinates and dimensions that you need to know where you can click. That’s the whole point of not having to search for the image again.

I hope that has cleared up any confusion.

1 Like

Ok…so to add a layer of confusion to it all. Would it be possible to get KM to click on the bottommost/topmost of either of the two images? My situation is this. I want KM to click on the lowest of those triangles on the right. With the current macro I have it just clicks on the first triangle icon because it runs that search first but it would be great if I could run the search simultaneously

Click on lowest of either image.kmmacros (48.0 KB)

I am perhaps not clear about what your aim is here, but it might help if I mention that you could get the co-ordinates for the lowest image by using a Find image on screen action that is set to look for the “bottommost” image within the frontmost window.

Then use the move mouse action to click at (or relative to) those co-ordinates.

1 Like

And can those images also be either way round -- that is, you don't know which will be highlighted? So you could have

ImageA
ImageB

or

ImageB
ImageA

...and you always want to click on "line 2"?

If so, I think you'll have to find the coordinates of both images and use whichever has the greater y coordinate. Demo:

Click on Lowest of Both Found Images.kmmacros (18.1 KB)

Image

1 Like

@Nige_S @kevinb Thanks for your suggestions! Wound up using a combination of the two to get it working for my setup.

1 Like