Looking for: "There is Text Selected" True/False

Is there any way to test for whether some text is currently selected (in the frontmost window)? Somebody else asked about this here but that solution assumes something's been selected and copied (so Clipboard can be tested for a changed state).

I want KM to tell me: Yep, there's some text selected there! [True] Or, Nope, you ain't got nothin' selected [False]. Just that simple.

To my knowledge, there is no simple way to tell that. Unless the app enables/disables a menu item like "Edit->Copy" - in that case, you can just check the status of the menu.

The only other thing I can think of is to clear the clipboard (set it to an empty string), do a copy, and see if anything shows up in the clipboard. You'd need to do it something like this:

image

ย 
Make sure the options for the "Copy" action don't abort your macro on failure (click the "gear" icon).

For the "Pause Until Conditions are Met" action, use the gear icon to set a very short timeout period, and don't abort on failure.

1 Like

If something is selected in the frontmost App then the Menu Item Editโ†’Copy will be Enabled. If nothing is selected the same Menu Item will not be Enabled. You could use this in something like this:

image

This would be for anything selected in the the frontmost App, not just text, but it might work for what you are trying to do.

EDIT - Just reread @DanThomas's reply to you and see he suggested this too.

The macOS accessibility API does not provide convenient access to the currently-selected-object. Hard stop.

Therefore all methods to do this are workarounds โ€“ unless an app is AppleScriptable and the selection-object is available โ€“ or you can get to
the selection-object with System Events and UI-Scripting.

So โ€“ it's very important to mention what app or apps you're working in.

EDIT by @_jims, 2022-01-07:

The version of the macro I posted previously had a bug. The use cases tested are:

ABC
XYX

  1. ABC (or XYZ) in Clipboard, no text selected, trigger macro, paste text.
  2. ABC in Clipboard, XYZ selected, trigger macro, paste text.
  3. XYZ in Clipboard, XYZ selected, trigger macro, paste text.

The previous version of the macro failed the third use case. :frowning:

I've updated this post with a corrected macro. I apologize if the previous version caused confusion.


Hi @rowan. I was working through this very issue when I was developing a macro named Augment or Filter Text. I discovered a few nuances during the process.

Here's a simplified macro that includes the relevant actions.

DOWNLOAD Macro File:
Get Selected Text- Otherwise Clipboard v1.1.kmmacros (16 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-Image

OK, good to know. Thanks.

1 Like

Thanks Jim, I'll take a look at your work when I can (it looks impressive so I need to prepare!)...
Rowan

1 Like

@Zabobon
OK, this could work. Thanks for the suggestion.

2 Likes