Find image relative to cursor position? [SOLVED]

I have this action that needs to find a certain image and according to this article, one of the tricks to increase the chances of KM finding that image is by setting an area on the screen.
In this particular case, this image can almost anywhere on the screen, it's not a fixed position. Is it possible to set that area relative to the cursor position, as if the cursor is like an "anchor"? That way I could say "find image that's within this area around the cursor"

If the cursor position is the same thing as your mouse pointer position, you can get the coordinates of that using the Token %CurrentMouse%
You could then use those numbers to set an area and use that area calculation in the Find Image Action.
Without trying it, I'm not sure of the exact maths but I suppose it would be taking the mouse x,y coordinates (which would be the centre of the area) then halving the width and height of your area
so that you can find where that area's top left corner should be... (I'm sure someone else here can give you the exact equation).

While you are building your Macro, to see what numbers the Token %CurrentMouse% is returning you could add a temporary Action to your Macro that displays them:

1 Like

Yes, that's what I mean :slight_smile:

I'll see if someone can jump in and help, as I'm not an expert when it comes to the deep intricacies of KM (yet)

That's a good trick that can be used for other scenarios. Thanks for sharing!

Click Image Near Current Mouse.kmactions (2.4 KB)

Action Screenshot


Okay, I think I've got the maths now. To get it to centre the search area you need to also minus half the width and half height of the area.


I've edited the action group above to be a bit more user-friendly.

1 Like

Awesome Neil! Thanks! :slight_smile:
What is the [1] and [2] and the -100?
I'm assuming the 100 is somehow related to the value 200 you picked for width and height? So if those numbers were 380 and 400, you would then use 160 and 200?
Or is 100 related to something else? (Looking at @Zabobon example it seems that's related to the values of W and H)
Curious to understand the logic behind all of that

When you set a variable to something,something you can access the individual parts of that array using variable[1] and variable[2]. As @Zabobon pointed out (no pun intended), to centre the search area around the pointer location, the top-left coordinates of the search area need to be adjusted by half the search area size.

@noisneil looking at what you just shared here (Click Found Image based on which image it finds - #4 by noisneil)
instead of using the -100 (or whatever number we need), couldn't we use the MidX and MidY as well? Or is that just related to images?

Mid.X and Mid.Y refer to the middle of an area. There is not yet a middle for this area as we're defining it now.

Referring to the area fields:

→ is the location of the left perimeter of the area
↓ is the location of the top perimeter of the area
←→ is the width the height

Oh I see. So according to this article (token:FoundImage [Keyboard Maestro Wiki]), you get that array (left, top, width, height, fuzz) so when you add [1] you are accessing the value for LEFT and using [2] it's for TOP, correct?

It took me a while to understand the concept and had to write it on paper so I could see it, but I guess I got it. Basically when you subtract those values, you are just shifting the area half of its width to the left and half of its height to the top, correct?

You got it!

1 Like

Ok, makes sense.

Well in that case, could we use
Local__Mouse[1] - Local__Mouse[1]/2
or something similar? So if we change the Width and Height, we don't need to do any math. Does it make sense? Does it work?

This deducts half the current mouse x-axis from its y-axis. Not all that helpful in this case.

Look at the updated action again. No maths necessary. Just enter your preferred search width and height and you're done.

1 Like

Oh sorry, I didn't see that update.
Yes, so basically the workflow is similar to what I was thinking, but done the right way :wink:

1 Like

@noisneil so in this particular case, since I'm looking at finding the image (not click it, for this particular action, because it's an IF THEN action), I would do this, right?

Also, do you know if it's possible to emulate the "DISPLAY" feature for this kind of scenario? For example I want to see the search area highlighted so I can make better decisions.
If I tick Display, it will just highlight the image it finds, not the area.

Is this possible?

If Image Found Near Current Mouse....kmactions (3.1 KB)

Action Screenshot

You can use the Action Highlight Location to make a rectangle show on screen. Interestingly, the example in the Manual also uses a different way to to get the mouse location... using MOUSEX() and MOUSEY() directly in the fields.

1 Like

Good catch! One less action needed. So here are the updated action groups:

Click Image Near Current Mouse.kmactions (3.0 KB)

Action Screenshot

If Image Found Near Current Mouse....kmactions (2.8 KB)

Action Screenshot