Performing an action on a selection seems too difficult

  1. Is there a reason it isn't more straightforward in KM to perform an action on an existing selection of text? For example, simulate a right-click and choose an option from the contextual menu?

  2. If so, does it simplify matters to put said selected string on a clipboard first?

The reason I ask is that in GUIs, there is so much emphasis on directing the machine's attention to something by selecting it, yet it seems as if KM has no way of using that particular context directly.

You can simulate a right-click with Keyboard Maestro, and you can drive the UI to get to items inside the contextual menu.

But Apple doesn't provide public APIs to many things in macOS, so Keyboard Maestro in many cases must simulate what a user would do manually – and it cannot conveniently do a number of things users would like to do.

Even so – despite all obstacles Keyboard Maestro is enormously powerful...

Feel free to be more specific about what you want to achieve – you might get more specific comment.

Feel free also to complain vehemently to Apple that their automation support in macOS is lacking.

@peternlewis – any comment?

3 Likes

There is no way in macOS to get the location of the current text insertion point or current selection, and there is no general way on the mac to get the contents of the selection (eg, the text that is selected), except via copying to the clipboard.

Absolutely.

Start your macro with the Copy action, process the text however you want and finish with Paste. If you want it to be selected, a trick that works with most applications is to follow that with Undo/Redo.

3 Likes

Hi @dharmanaut, just so you know for sure here's an example macro that brings up the contextual menu and chooses the "Copy" action...

Test Contextual Menu.kmmacros (2.8 KB)

Click to see macro

Keyboard Maestro Export

By way of explanation:

  • the first action just performs the right mouse click to bring up the contextual menu
  • the second action (a short delay) allows time for the menu to be displayed
  • the final action types CopyReturn to choose and action the menu command

This approach can be used on most contextual menus but obviously you'd need to change Copy to whatever you need to do.

Hope that helps.

2 Likes

Peter - thank you. I imagine Apple does that for security reasons. Anyway, I nominate your observation here as a candidate for a pinned Top 20 Things To Understand About Keyboard Maestro.

2 Likes

I doubt it - there has never been a way to determine it, and the focus on security restrictions on things like this is a relatively recent change (maybe the last ten years or so). Before that there was an assumption that the user knew what they were doing, and the applications they had installed were trusted to do what they said they would.

Now they might use it as a reason not to do it, but really it should be possible for accessibility reasons.

1 Like