How can I know whether a string is selected or not

Hi, I am a new comer and I don’t know if this is the appropriate place to ask this question.

I want to add some changes onto a selected string (I mean a selected word) for example on Pages or on OpenOffice writer, but when, by mistake or other, I have not selected any word beforehand, I want to quit the macro showing a message like “No string selected!”. Please let me know this technic explaining in a plain English because I am not good at it nor at macro programming.

Hi @Pepeino,

Not to worry, this is a fine place to ask this kind of question. The answer to this question may be a bit advanced for a new user, but I'll do my best. The most reliable way I've found to accomplish something like this in Keyboard Maestro is with a function called CLIPBOARDSEED(). Here's an example macro that shows how to use it:

[Example] Check if String is Selected.kmmacros (3.6 KB)
43 PM

To quote the KM wiki page on CLIPBOARDSEED():

The CLIPBOARDSEED() function returns an integer that changes when the clipboard changes. It will never repeat, so you can use this to test when the clipboard has changed.

To do this, we use two calculation variables: one before a Copy action, and one after, with each one set to CLIPBOARDSEED(). The Copy action is set to time out quickly, and to not end the macro if it doesn't actually end up copying anything. We then compare the two variables to see if their values are identical. If they are, we know that nothing was copied, and if they are different, we know that something was copied. In other words, we can use this to tell that whether a string was selected before the macro was run, since if it had been selected, it would have been copied, and CLIPBOARDSEED() would have changed accordingly.

This may be easier to understand in practice, so feel free to use that example macro and replace the purple placeholder action with the actions you want to use to change a selected string. If no string is selected, the macro will show a "No string selected!" message as you described. Good luck, and feel free to ask more questions if there's anything you want to know more about.

Hi gglick, thanks for your prompt reply.

Hmm, fairly complicated for me. Here, sleeping time is near so I’ll study your example macro well tomorrow. Thanks a lot again, for the moment.

I have been using the same technique for years. After having tried numerous other methods to determine when something has been selected (text, image, object), I found using the CLIPBOARDSEED function (KM Wiki) to be the simplest method which is also the best, most reliable method.

See:

MACRO: [CB] How to Detect When Clipboard Has Changed [Example]

Basically this works by get a unique value of the clipboard (the seed) before and after a copy. The clipboard seed will change ONLY is something was selected and copied.

You can use this in several ways, to make reuse easy:

  1. Use this as a Sub-Macro, calling it using the Execute a Macro action (KM Wiki)
  2. Store the Actions in the [KMFAM] Favorite Actions and Macros system by macro master @DanThomas.

Questions.

Thanks, JMichaelTX and gglick,

Now I think I understand well how the function CLIPBOARDSEED() works, and where and how to use this technique.
Many thanks again!!