How to Verify That There is an Icon Under the Mouse?

I'm working on a macro to automate a small workflow. The workflow starts with right-clicking on an icon to use the "Open With" menu. The workflow will be triggered by a hotkey. For purposes of right-clicking, it doesn't matter whether or not the icon is selected.

Is there a simple way to determine whether or not the mouse is over an icon?

The best I've been able to think of is to click to select it and then run an AppleScript to find out if there is a selection and if the selection is the right kind of object.

I'd rather have a simple, direct KBM query instead of kicking off AppleScript, but I don't know if KBM can answer the question. I haven't been able to formulate a search in the KBM manual that comes up with anything useful.

Any ideas?

How about this?

Mouse Over Image?.kmmacros (80 KB)

Macro screenshot

  • Take a screenshot of the icon and make a mental note of its size in pixels as displayed beside the screenshot tool crosshairs.
  • Add these values to the green actions and the screenshot to the If/Else image well.
  • If your mouse is over the icon when the macro is triggered, you will see YES appear in a notification; if not, you will see NO.

NB: The red action will highlight the searched area when enabled.

...or this:

Get File Name Under Mouse.kmmacros (24 KB)

Macro screenshot

  • If the mouse pointer is over a file icon, you will see its filename appear in as a notification; if not, you will be notified that the mouse pointer is not over a file.

Note that this is slower than the found image option, but more universally applicable.

Aside: Why use a right click at all when you could use KM to Open With for the Finder selection?

I'm not looking for a single, specific icon, I want to verify when I click the hotkey, that the mouse is over some icon and if it is, then I select it and OpenWith, etc. and if it isn't, then I do something else, maybe nothing.

Because I didn't know about it and have been trying to get KBM to mimic how I do it by hand.

It seems like the Open With action requires that I know the filename of the icon. How do I get that other than selecting it and running an AppleScript to tell KBM the name of the selection? That's what your second macro does.

As I said in the OP, I'd rather a simple, direct KBM action instead of having to invoke AppleScript and your comment about the second macro being slow is exactly why. Thanks for that confirmation.

I checked the manual again and this time I found an entry for "The Open the Finder Selection action" which was last modified 2015/07/07 and there is no action by that name anymore. I may have stopped there, last time.

This time I dug deeper and it looks like that action has been replaced by the "For Each Item in the Collection Execute Actions" action which has an option to use "The items in the Finder selection" and for each of those items, "execute the following actions", which could then include the Open With action.

I'm OK with not verifying what's under the mouse while the mouse is merely hovering.

I can click the mouse and then verify what has been selected, if anything. So I think I can use "The items in the Finder selection" collection and check whether there is anything there and if it/they are appropriate files.

I'm presuming here that if I press the hotkey while the mouse is not over an icon, that clicking at that spot won't select anything that would then appear in the "Finder selection" collection, so the For Each action won't run.

I haven't tried it yet, but I think that's the direction I need to go. Thanks for the nudge.




A bit of context would be good. What are you trying to do, more broadly?

1 Like

Thanks. Some questions:


There's no Token for Selection, so even if I use a variable, I have to know the name at some point to set the variable, or get it from AppleScript as in your previous example. Even if the name is not in my wet consciousness, my code has to know it, figuring it out from something. What else could work?


Yep. Don't know why I missed it before.





When I couldn't find the simpler action, it seemed to me that I could do what I wanted as a single instance of this action. So it's not a replacement because the original never went away. It's another way of doing the same thing as a specific case of a more general macro. That may still be useful at some point, like if I want to create a macro to be able to process multiple selections instead of doing them one at a time.


I started out trying to mimic what I do manually to select an icon in my specific .../DeskSpaceIDs/ Finder window, right click, pick Open With, open it with my DeskSpaceID app, which will change desktops, and then do some other stuff. Now it seems there are KBM actions that do exactly the step that I'm trying to do, and I don't have to do it with mouse actions. That's much better. Thanks for your help with this.

I haven't yet figured out in the Xcode app how to get the DeskSpaceID app (TextEdit clone) to automatically save an edited file with a custom extension, e.g., *.dsid, if I edit the file with and then save it, the app automatically appends .rtf onto the filename (if it's not there already). So then it doesn't automatically open in the app next time. So I've been trying to do everything with macros and hotkeys and "Open With" so that even though the file is stuck with an .rtf extension, I still open and edit it with

For example, I have a hotkey to open an RTF file with, but that macro is only active if my DeskSpaceIDs folder is front and active. For any other context and any other file extension, it opens the file with the system default. So if I forget and use the hotkey on a non DSID file, it still behaves reasonably. If I get the app working as I want it to, that problem goes away.

As soon as I get the file extension part of the app figured out and can get my TextEdit clone to save as *.dsid instead of *.rtf, it will simplify all of this. But I don't even have a trailhead on it yet, so I'm pursuing this workaround. Hence my obsession with all things "Open With".

To summarize:

My initial approach was to simulate with KBM the actions that I was doing manually. Hence, I thought that before right-clicking on an icon, I should verify that there was an icon there to accept the right-click. I wanted a right-click to be able to access the Open With menu. But KBM has a built-in "Finder Open With" action which will do what I want in a single step and has all the proper validity checking built in. It won't run if there is no selection in Finder.

There is a token: %FinderSelection%

There's also %FinderSelections% which returns multiple items as a list.

And many many other things... You should probably start using Local variables if you don't already (you're using a Global one in your example). Read up on the wiki. It keeps your variable list lean.

Foo! I was sure that neither of these showed up when searching the Manual/Wiki for "selection". But when the Wiki UI repeated my previous search, it turns out that I had misspelled "selection". Operator Error!


Top tip:

Control-Command-T brings up the token list.

(Pretty sure that's the key command; I'm not by my mac right now)

Yes, that works. I remember being able to click on the [T] icon in the right corner of the text input window, but that is not working for me now.

So I guess I can test whether or not that token has a value that can be used in a future step. I will also want to reselect the same file later, but I don't know how to turn a file name into a Finder Selection.

Here's what I want to do, still working the OP question:

  • In a Finder window, manually select a file.
  • Open the file, which will possibly change Desktops.
  • Get some info about that (new) Desktop.
  • Reopen the original Finder window.
  • Open the Get Info dialog for the previously selected file
  • Paste in the info gathered from the (other) Desktop.

I don't know how to open the Get Info dialog for a file by filename, only when it's selected in Finder, so I think I need to be able to select a file in Finder by it's filename, and I don't know how to do that either.

I think that, as in the OP, if I could verify that there is indeed an icon under the mouse, no just a file selected, then I could save the mouse coordinates so that when I return to the Finder window, I can reselect the same file.

Any ideas?

My original concern had been, what if the mouse moves between selecting the file and pressing the hotkey, then the mouse coordinates at the time of the hotkey press would be invalid. So I framed it as verifying the mouse position.

Here's my current thought about verifying the mouse position:

  • Manually position the mouse over an icon in the Finder window. Click or don't, it doesn't matter.
  • Manually press the hotkey as a trigger. The hotkey does the selecting, not any optional, previous mouse click.
  • The macro saves the mouse position
  • Macro clicks at the SAVED mouse position, not where the mouse may be by now.
  • Test if the click selected anything by testing if %FinderSelection% has valid contents. If so, proceed, if not, ignore the whole thing or give an error message.
  • If proceeding, then the saved mouse position will allow the same file to be reselected after doing what we do with the file the first time around.

If I run into any implementation problems, I'll come back here, but for now I think that's the solution to what I was asking in the first place.

You don't need to know how to open the Get Info window. What you need to know is how to store some information in a file's Comments attribute. Keyboard Maestro has actions to get and set file attributes.

I have a feeling none of this is necessary and that we're looking at an XY Problem.

Thanks, that's very useful and I expect to be using it. Looking at that in the Files Action Group also let me find the "Reveal a File" action.

However, I think I do need the Get Info window in order to be able to paste in a custom icon for the file.


The answer I marked as the solution will be going untested because I have found that it is no longer necessary to perform the OP verification that I thought I needed.

I originally thought that I would need a valid position for an icon to be able to open the file and do things and then return to the Finder window and right click on the icon to access the Get Info window and set the file's Comments and Icon. Even when I found that I don't need to right-click, that I can just select the file and use the ⌘I hotkey, I thought I would still need to be able to trust that I had saved a valid mouse position before using it.

But now that I can set the file's Comments and Icon with the filename, using the two previous actions posted by @noisneil, I don't need to open the Get Info window, so I don't need to access the file's icon a second time.