Find an Image on Both Light and Dark Themes

I have a macro that depends on find image on the screen. I built it during daytime when screen is in light theme, it worked ok. When at night, Mac changes to dark theme so the macro failed. The easiest way I think I can just duplicate the macro and use dark theme image. Apparently this leads to some kind of duplication. Another way is to use nested if then else. What's the best way to handle this? Thanks.

No no no! There's an easy way to fix that. Let me post an explanation...

I posted a solution to this problem on another thread, but since I can't find it, I'll repeat it here...

Step #1 of the trick is to simply perform two consecutive searches in a row. One search is for the light image, and the other search is for the dark image. Both searches use the same variable. Step #2 is to turn off error messages/detection for both of the searches. Step #3 is to search for the second (dark) image if there was not result for the first (light) image. Here's an example. If you do this, then the rest of your macro simply follows these statements. You don't need two macros or any duplication.

P.S. I'm not sure if you can tell from these two images below that the are different colours. One is from Light Mode, the other is from Dark Mode. In these images, they look similar, but they aren't.



Thanks. This is similar to what I had on the second option to use nested if.

Okay. I didn't realize that's what you meant there, because it's just an IF statement which isn't nested in anything. Sorry.

1 Like

your solution does help in case need more than 2 images need to be found :slight_smile: also nested if is not easily read.

If you don't like the IF statement, you can replace it, as follows. The new variable, MyImages, will contain a single image search result.

I haven't work with image variable yet. After image is found and saved in variable, is it possible to perform a click on the center of found image using the variable?

The only type of variable is a string variable. There is no such thing as an "image variable." The string returned from a Find Image search is a string containing a list of 5 numbers described on this page:[]=find&s[]=image

The X,Y coordinates represent the upper left corner. Typically when I want to click on the centre of the image I just add 10 (+10) to the X and Y coordinates. Usually I find "10" is good enough. If you want to get the exact centre, it's a lot more work. You won't like the extra work.

The page above seems to indicate that the Find Image action used to return 7 numbers with the extra 2 numbers being the centre of the image. It doesn't work that way now, if it ever did before. Maybe I'm misreading it.

@henryone follow @Zabobon ;s advice, linked here:

1 Like

Thanks. That was the post I was looking for. Of course, it works only when you don't need to save the location into a variable. I'm not sure if Henry really needs a variable, although he did mention that at one point.

Thanks both!

1 Like

The MidX/MidY notation described on the wiki page seems to work just fine. Here's an example macro to demonstrate:

Found Image Token Example.kmmacros (35.2 KB)

In my tests, that macro moves the mouse right to the center of the KM icon on this forum. My best guess is that, even though the Find Image action only returns five numbers, the MidX/MidY notation automatically performs the math using the width and height values to derive the center.


Wow! I never would have guessed that. I just assumed the documentation was out of date because it didn't match the results I was getting. If the documentation was more correct, this could have saved me a lot of work over the years.

I've tested MidX and MidY works. so in absolute position, x+MidY and y+MidY is the center of the found image.