Get the selection quickly even if empty

I'm using a few text macros operating on the current selected text by replacing it with something else. I do this by:

Then I work on text (ususally calling sed in an Execute shell script action) saving the outcome to a variable res which I then type out:

So far so good. This works fine if there is a (text) selection. However if this selection is empty the first action waits until it times out then aborts.

I know I can customise Timeout time and Abort behaviour, but what I really want is when there is no selection just a quick setting of the named clipboard to "empty" or something like this.

There's a KM function called CLIPBOARDSEED that can be used to verify if a selection is empty or not by checking whether the clipboard changed. Here's an example of how it can be used in the use case you describe, albeit with the system clipboard instead of a named clipboard:

Check Selection.kmmacros (7.9 KB)

Another example of this functionality can be found here: How to Detect When Clipboard Has Changed [Example]

Hi Gabe

Your macro looks pretty sophisticated! :slight_smile: Thanks for this.

The thing I don’t get is: Why do I have to do the cut in the first place? Can’t I just replace the selection?

Hey Leo,

You’re welcome! I hope it proves useful for you. Depending on the app you may not have to perform a cut (I usually use a copy myself, but I used a cut here because you used one in your original macro) but you do need to get the text into KM somehow, and a cut or copy operation is by far the easiest way to do so (plus, the CLIPBOARDSEED function used to check the selection here doesn’t work otherwise). If you add actions to delete the past clipboard(s) once the final paste is done, the macro can essentially function like a replace.

Ah, yes, I’ve played a bit more with CLIPBOARDSEED and now I understand I need to do the Cut or Copy to (possibly) increase the seed number. Thx again.