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.
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:
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.
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.