Action to Right Click (Context Menu) on Selected Text, Irrespective of Application?

Monterey 12.6.4

A recurrent issue is my inability to have KBM right click (context menu) on selected text, irrespective of the underlying app, giving me access to the services menu for that selected text.

I am currently trying to find a solution using Bear 1.9.6 , but my question would also concern other apps.

Thanks in advance for your time and help

You can play with click at found image using the text highlight color, but this is a very finicky solution. Definitely not guaranteed to work everywhere.

1 Like

thank you. I tried all kinds of found image options but could not find any reliable solution.
Would the master @peternlewis consider a set of actions based on "selected text" ?

That's not really possible – the macOS doesn't have an API to work with the text-selection, so there's no consistent means of access.

With various apps you can use UI scripting or vanilla AppleScript, but once again there is no consistent means of access.

1 Like

To make the same point a different way, "selected text" is defined differently by each application – there is no standard macOS definition.

1 Like

macOS does not have any way to determine the location on the screen of the text selection.

I wish it did, but it does not. Some applications may reveal it via AppleScript, or Accessibility, but there is no way in general to figure out where the currently focussed text selection is.

You will need to find a different solution to your problem.

Pretty much the only thing you can do with the text selection is:

  • Copy
  • Cut (Copy & delete)
  • Paste (replace with the system clipboard)
  • Type (replace with text)
  • Any menu action that is available that operates on the selection (including the Services menu, which in turn can trigger a macro exported as a Text Service).
1 Like

Instead of using the selection, would there be any way to call up the context menu (right click equivalent) to the clipboard?

Thank you Peter

@ccstone @peternlewis @ComplexPoint

Would there be a way to identify selected text in Markdown (Bear is based on markdown)?

@ronald I probably totally misunderstand what you want to do. But assuming you want to open the services menu and select something, doesn't that work for you?

1 Like

No. Once again you'd have to have an API that supported working with the selected text.

You can use the Services menu in the App-Name menu.


Download: Menu ⇢ Services Menu.kmmacros (2.1 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

** Note that this works with any frontmost application, although I've had to special-case Google Chrome due to Google's shenanigans.

This is imperfect due to the way macOS works in that the Services menu doesn't always populate the same as the Contextual menu, but it might be sufficient for your needs.

I often find it necessary to open the Services menu before selecting an item in it due to Apple's laziness.

This frequently affects keyboard shortcuts assigned with the System Keyboard prefs as well – keyboard shortcuts in the Services menu often don't work correctly until the SM has been opened once.

2 Likes

you are right and thanks very much.

thanks very much for the macro !. It would be perfect, but for the fact that the item that I want to use in the context menu is Apple Translate which is curiously absent from the Services menu.

My question is beyond the scope of this forum. Would you have any idea on how to add it to the services menu ? I don't see it in the listing is sys pref.

How to use Translation in macOS Monterey | AppleInsider

Maybe this will help. Select your text, then trigger this simple macro.

One more detail: If the trigger (below) is used, the macro starts as soon as the mouse is released after selecting the text (with opt down). So you don't need a shortcut (eg ⌥A).

Ah, probably something like that is also feasible with KM. I can't do it, but @ccstone or someone else certainly can.

  • Search in services menu for a specific item.
  • If the item is there, trigger it and exit the macro
  • If the item is not there, activate the context menu and search for the item.

At the risk of looking a bit silly, I found this:

I don't really know what's going on here, but I do know KM supports Swift, so I wondered if there might be a way to return the coordinates of the currently selected text?

I'll get my coat...

Oh, and I also found this, but I think it's a Cocoa (whatever that is...) thing.

You can't directly -- things have to be "wrapped up" as a Service.

But you might be able to do something similar using Shortcuts, which includes a "Translate" action. If you create it in the "Quick Actions" section it'll be added to the Services menu and the input will be the selected text when you trigger it. Something like:

...will display the translation in a dialog, I'm sure you could do other things with it.

Note: I'm even more of a Shortcuts newb than I am a KM newb, so I'm really hoping someone will step in if you've got questions!

2 Likes

I've tried that API before I'm afraid. In theory, it should be possible. In practice, it does not work reliably. It might work in one app, but not another, in one field, but not another. When I mentioned Accessibility in my original post, that was what I was referring to.

3 Likes

Just a note to say that I was very impressed by your out of the box idea.

I am reading about Shortcuts and quick actions and will give you an update.

Thanks a million!

I really am a newb with Shortcuts -- but while the above works as a Services menu item, the little I understand suggests you should be able to do a more "normal" automation using the selected text and Copy-process-Paste either in a Shortcut or via KM and the "Execute Shortcut" action.

1 Like

I fell off my chair...

I read about the quick actions and everything you can do with them. A new and promising avenue for automation.

I tried shortcuts a few years ago on iOS but stopped because found it too limited. It has much improved since.

Thanks so much.

image

Note that you can export Keyboard Maestro macros as Finder Quick Actions or Text Services.

5 Likes