How Do I Tell a Macro "If the Field Is Empty, Then Move On"?

Hello and thanks for reading this.
I created a macro that it searches and replaces characters in 1st field, then moves to the next one and so on.
It works OK as long as the field has some text in it. When the field is empty the macro stops.
How can I tell the macro "If the field is empty move on to the next action"?
Thanks in advance for your help.

If you could post your Macro it would be much easier for us to help you.
See How to Post/Upload Your Macro to the Forum

Keyboard Maestro “3 Clean highlighted text and paste it back” Macro

3 Clean highlighted text and paste it back.kmmacros (11 KB)

@Saul - I can't replicate your error. When a match isn't found, things continue as per normal in my own testing. Might be something else going on here.

If you do need a test, something like this ought to work...

Thanks iNik.
I need to leave in 5. Will be back in the evening and post how it goes.

Thanks for posting your macro.

I believe the reason it stops is because the COPY action aborts due to having nothing to copy:

You can change this COPY action as follows:

And then insert this Action immediately after the COPY:

Of course, you can adjust the IF/THEN Action as needed to fit your workflow. The key is testing the `%ActionResult% token for "OK". If it is NOT "OK", then the copy did not complete properly.

Here is your macro with the above changes:

TEST 3 Clean highlighted text and paste it back (Saul).kmmacros (12 KB)


Hi iNik.
First, let me thank you "properly" (in a matter of fact you and all many others who are willing to devote their time and knowledge, so "ignorant" people like me will be able to reach somewhen a higher level of understanding and working w/KM). So, thanks a lot! I really appreciate your (and as said all others) help!

Already from your reply I learned a bit more in an area that is pretty hard for me to understand so far: the whole area of variables and how to work with them.

Now, to your answer:

I integrated the short macro you posted and "put it to work" with my example.
I am working on replacing characters in names in different languages of classical music pieces.
For this example I took the text "Impromptus, Op. 90, D 899 - #3 In G Flat" and tried search and replace the "#" character.

This is the new macro:
Keyboard Maestro “Test for cleaning text” Macro

Test for cleaning text.kmmacros (4.1 KB)

Now, when the text "Impromptus, Op. 90, D 899 - #3 In G Flat" is highlighted everything works fine and the "#" is replaced with "no".
When the returned text ("impromptus, op. 90, d 899 - no3 in g flat") was highlighted and the macro triggered. everything was OK the macro worked and I got the glass sound (no "#" to replace).

But then I triggered the macro when no text was highlighted. Nothing happened for about 10-11 sec. then the message

I use this macro as part of a bigger macro and whenever the macro gets to a field with no text, it aborts the macro after the previous message.

Hi JMichaelTX.
Sorry. Left the house for the day, the reply to iNik was in work and when I got home I finished it and hit “Reply”, then I found out you replied too in the meantime.
(Please read my thanks to you in the beginning of the above reply) :slight_smile:

I tried your macro and it does exactly what you said. My question is: since this macro is part of a bigger one, how do I avoid the aborting action and instead tell the macro “Well, if there is nothing to copy, just keep on, go to the next step” (something like “=if(A1=”","") in Excel)

Remove the Cancel this Macro Action, and in the next field on the If/Then Action, "Otherwise execute the following actions", put all of the below actions you want to execute when there is something found by the COPY action.

You may also want to either disable the "Display Text" action, or replace it with a Notification action.

BTW, you may want to look at using the For Each Action instead of all of the individual Search/Replace actions.

1 Like

Hi JMichaelTX
I am trying to understand and implement what you wrote to me.
But tomorrow I’m taking my MBP to the Apple store and it will be there for some days. (Just didn’t want to give the feeling I disappeared) :slight_smile:
Whenever I’ll be on again I’ll post here again.

1 Like

Hello JMichaelTX,

First I owe you (and the forum) an apology. I started this thread more than 5 years ago. My laptop died, bought a new one reinstalled everything from scratch and completely forgot about the thread.

Now I got back to work on the Music app, had the same question and when Googling came across my thread.

So, just to wrap up properly and in short: JMichaelTX, you were right, the problem was that the macro aborted when the field needed to be copied was empty and your answer (which I checked as the solution) solved it.

People say “better late than never”, so, “a bit” late: JMichaelTX thanks a lot!!! :slight_smile:

Happy Holiday season everybody! :christmas_tree:

1 Like

Hey Saul,

Sadly @JMichaelTX will not be answering...

The Passing of JMichaelTX

JMichaelTX – Unforgettable Generosity With His Time and Knowledge


1 Like

ccstone, thank you for telling me and for the links.
I have tears in my eyes, cause although I didn't know Jim personally, I've always felt his warmth through his answers. Always felt that he cared.
I feel like I've lost a friend.
R.I.P. JMichaelTX!

1 Like