Specify Closest Image on Move and Click at Image Action

I love the Move and Click action that lets you base it on the location of an image. But if the image appears more than one time on the screen it fails. :frowning:

Is there a way to tell KM to use the image closest to the current mouse location?

TIA.

No.

The best you can do would be to use the For Each action, the Found Image collection, and then find the best match yourself by some means.

Alternatively, you could try something like this (I have no idea how well it would work)

  • Find Image in area MOUSEX()-100,MOUSEY()-100,200,200
  • If variable is empty
    • Find Image in area MOUSEX()-200,MOUSEY()-200,400,400
  • If variable is empty
    • Find Image in area MOUSEX()-300,MOUSEY()-300,600,600
      etc

However, don’t try this in 7.0 as the Area selection immediately crashes the editor (oops), so wait until 7.0.1.

You can try finding the image only in the front window if that helps.

Thanks for the clarification.

From the wiki, it appears this is only available in KM7, correct?
I don’t see any way to do this (Find Image or Move & Click) in KM6.

No, I’m afraid I added a bunch of things to the Find Image system to allow for this sort of thing which was impossible to do in 6. Sorry.

“Find Image in Area” must be a KM7 new action, as I can’t find it in KM6, and as suggested by your post below.

Yes, its new in version 7 (and crashes as soon as you try to select it in the editor, sigh, 7.0.1 coming soon).

Oh well. We’ll give you E for Effort for now. :laughing:

Seriously, this will be a great Action.
I have just finished upgrading to Yosemite as we speak (err, write), so very, very soon I’ll be on KM7!! Yea!!

1 Like

Hi as a user coming form Autohotkey it is quite hard for me to understand why the “click first image found” feature is not implemented in Keyboard Maestro. Autohotkey has this feature in the ImageSearch command. The way it works there is it starts scanning from left to right, top to bottom, and than gives you the first hit as the result. It can be also configured if you want the 2nd, 3rd, etc hit. Such a feature would be very useful in Keyboard Maestro, as it is more typical (I think) that the user wants to click the 1st / 2nd / 3rd / etc hit, instead of not clicking anyhting.

Also did anyone find already a working workaround how to click the 1st image found?

Hello Giny,

Features get implemented if/when system APIs allow, and if/when the developer wants to spend the time.

AutoHotKey is a very mature utility and has a kitchen sink full of features.

As it happens this feature made it into Keyboard Maestro 7.

For Each.kmactions (17 KB)

The image in my macro probably won't work for you, because of the specifics of my system – but it's a folder on the Desktop.

What is displayed in the window is the (left,top,width,height,fuzz) of the first found image.

You can use those in a click action.

You can break out items from the list using a Set Variable to Text action and performing a calculation – the bracketed number is the position in the array.

%Calculate%imageVar[1]%
%Calculate%imageVar[2]%

Using the calculation directly in a Click action requires a little different syntax:

So. You should now have enough information to construct your macro.

-Chris

https://wiki.keyboardmaestro.com/collection/Found_Images

2 Likes

Great tips Chris,
Thanks for sharing them. It was really useful!

This is what I created...

This moves the mouse to the middle of the first found image.
Of course you can change the action to click instead of only move there:

My Screenshot:

My Script: MoveMouseToFirstFoundImage-giny8i8.kmmacros (38.6 KB)

As I am not a real good programmer, I still miss one thing:
How can you move the mouse to the n-th (2nd, 3rd, etc) occurrence instead of the first one? In other words, how to break the loop after the second, third, etc run?

Hey Giny,

Use a Set Variable to Calculation as a loop-counter.

Psuedocode:

```AppleScript`
set variable myCntr to 0
set Variable to Calculation myCntr = myCntr + 1

if-then myCntr = 3
break from loop
end if


If you have problems working out how to do this holler.

-Chris

Hello everyone, I just wanted to thank those in the thread for making a task super-simple for me in my workflow: Really is great to have these sharp minds on here willing to help. giny8i8 Your solution to this is clean and efficient and definitely worth a thank you! Regards, David.

1 Like

I'm not sure exactly which KM version added this, but at least as of Keyboard Maestro 7.3.1 (7.3.1) on macOS 10.11.6 this can be simplified with these two actions.
Note that you can now choose whether or not the image has to be unique on the screen.
Also, the .MidX and .MidY functions make things simple.