Check if selection exists

Why doesn't this work if no selection exists?

The macro keeps running for a long time and then an error message is displayed.

2024-02-17 16:18:30 Action 15613718 timeout exceeded. Macro “Test - Check if selection exists” cancelled (while executing Copy).

You are assuming some connection between selection (you mean in the Finder ? In a text editor ?),
and clipboard ?

In some contexts, copying the foreground application's selection will, for example, populate the system clipboard with graphic or other pasteboard items, and will create no textual pasteboard items.

I’m working in an editor. Not assuming that there is a connection: I want to use the Copy action to check. If nothing is selected, the Copy action should have no effect.

Even text editors vary in how they populate (or leave) the clipboard when the text selector is collapsed.

I tried to set the clipboard to a string and test for that: same result.

I use BBEdit.

Here you're talking about a selection, but your macro has nothing to do with a selection. Can you explain what the objective of this macro is, in terms of what it should do in your workflow?

Checking the clipboard definitely works; I use this step in my decrufter macro to make the macro quit if there's no URL to process:

-rob.

To check if there is a selection, look at the Edit menu to see if Copy is enabled:

You can detect a change in the Clipboard if that's what you want. See How to Detect if Clipboard Changes?.

But it isn't clear what you're trying to do.

First I set the clipboard to empty. Then I copy. If there would be a selection existing in the editor, the clipboard would no longer be empty, right?

I don't think that's correct. A selection is not necessarily copied to the clipboard.

As for "emptying" the clipboard, see How to Do if Condition on Empty String of Clipboard.

The thing was: you cannot use the Copy action for this. You actually have to use to simulate Cmd+C ...

1

What's the name of the application?

BBEdit

The test I showed you above to see if any text is selection does indeed function in BBEdit. That's where I use it all the time, in fact.

In addition to the Copy menu option, you can also monitor the Paste option to see if there's anything on BBEdit's clipboard to paste into your text.

1 Like