UI Selection

Is there a generic way to test if a selection actually exists… apart using Copy action fail, waiting the timeout?

–Alain

1 Like

Could you test for an enabled Edit->Copy menu item? It would depend on the app, of course. Some (like Chrome here), don’t change the menu.

The only way I have found to deal with this issue, is to

  1. set the Clipboard to something known, like "[NONE]",
  2. Do a ⌘C (which will fail quickly if no selection)
  3. Check the Clipboard to see if it is "[NONE]"

I use this a lot, and it seems to work well.

Here's an example:

@DanThomas Perhaps I am not clear enough but I address screen selection. Thanks anyway for your answer.

@JMichaelTX Thanks and good point with ‘[NONE]’ in clipboard. Is there great differences between Copy Action + timeout on one hand and Type C + Pause one the other hand?

@peternlewis Peter is such selection monitoring technically conceivable?

Is a selection trigger can be at least imaginable?

[Edited 2016/05/24/05 18:49:29]

–Alain

This is in TextEdit. Nothing is selected, Edit->Copy is disabled:

Here, some text is selected. Edit->Copy is enabled:

So, for TextEdit at least, you can use the following to determine if anything is selected:

Unfortunately not all applications disable Edit->Copy when nothing's selected. But if the application you're dealing with does, this will work, and work FAST.

That's why I just use the KM Type a Keystroke with ⌘C because it fails immediately if nothing is selected, and works in all apps.

Ah, I missed that - I thought it took a while to time out also. That's what I get for speed-reading the thread. But at least I learned something!

With the Copy Action you have to worry about both timeout and providing enough time for the app to complete the copy. I have tried Copy and found it just does not work as easily and well as doing a keystroke of ⌘C.

Not answered :wink:

Not really.

-Chris

Other that testing whether the Copy menu is enabled (which may or may not work depending on the application) or testing for a selection via AppleScript (which will rarely work and depends on the application), no.

The Copy action waits for the clipboard to change. To essentially it is a Pause Until clipboard changes.

Not that I can see.

@ccstone, @peternlewis Thank you.

As I (believe) understand each application knows only about its own selection.

KM can only use indirect information about selection in other application but itself: so I will restrict use of selection to KM :joy:

–Alain

Hey Alain,

Right. Unless the app in question publishes information about its selection.

tell application "BBEdit"
  tell front text document's text
    set mySelection to the selection
    set selPropList to properties of the selection
  end tell
end tell

Not many apps offer this ability, and it is not universal either – it cannot be used in various fields and dialogs for instance.

-Chris

The free sister application “TextWrangler” also gives information about its selection: potentially useful, as it is my universal editor :wink:

Thanks Christopher for your script.

–Alain

Alain, are you having problems using my technique of setting the clipboard to "[NONE]" first, and testing after a ⌘C? I have found that it works find in all apps.

1 Like

General information and all experienced users advise about a main feature as selection is useful (needed).

JMTX in your universal solution I just worried about the completeness of the clipboard load for large selection with a Pause of (arbitrary) 0.2 seconds.

So my tentative (synthesis) solution, taking the best of each worlds is finally:

With this code the arbitrary duration Pause can be anyway very small and independent of selection size...

-Alain

Good point, and your solution looks reasonable to me.

I have found for 99% of my use cases a 0.2 sec pause is succificent, but since these KM Actions are in each Macro, I have sometimes tailored it to the specific use for that macro, if I am expecting, or want to allow for, a large selection.

My primary use case is to make the macro work with, or without a selection, making it easier for the user to use.
EXAMPLE:

  • I have a Styles macro for Evernote, which will either apply the chosen style to the selection, or offer to auto-select one of the following AFTER the current cursor location:
    • Word
    • Line
    • Paragraph

So, in all of these cases, the size of the selection is small/modest.

What drove me to use ⌘C vs Copy action is the long wait/timeout of Copy. But your solution deals with that nicely. Knowing that the KM Copy does a pause until clipboard changes helps a lot.

1 Like

A post was split to a new topic: Copy with Selection Test [Sub-Macro]

All this is very interesting, but I am missing something basic: if there is no selection, Copy fails and the macro is exited. Seems to me that what’s needed is the equivalent of a “try” around the Copy. I don’t understand how anyone is getting any results from Copy when there is no selection.

Can’t AppleScript it because applications don’t necessarily publish their selection. I am trying to tell if there’s a selection in a TextEdit document.

Yep, that's the point -- to know that in fact the user has NOT made a selection, when the calling macro requires a selection to process.

I've used this technique many times to make a default selection if the user has made none. For example, I have a google search macro that if there is no selection, assumes the cursor is at the beginning of a line of text to be selected and used in the search.