Insert Text: How do I create a condition where "If a variable is empty, don't insert a new line"

I want to create a simple macro which prompts me to type in a bunch of values and then outputs a block of text using those values. Just like a TextExpander macro.

My problem is, sometimes I do not type in all the Order and Volume variables. When the macro runs, those empty variables got turned into empty lines.

How do I set up something like "If this variable is empty, when inserting text, do not create a blank line and just go to the next one"?

Thanks. I hope I've explained it clearly.

While it is possible to create if→then actions that can be told not to include empty variables, in this case, I think the simpler solution would be to search and replace your text after inputting your values to get rid of all the empty lines. Feel free to copy the search-and-replace action used in this sample macro if you find it works for you, and please note that you will need to set your assembled variable values to a new variable or the clipboard (and use the appropriate search-and-replace action depending on which one you go with) before pasting them to make use of this solution.
Sample "Empty Variables" Macro.kmmacros (2.5 KB)


1 Like

Yes! That works perfectly!
Thanks a lot! Seems like another reason for me to learn regex.

1 Like

Glad it worked for you! If you're satisfied with my solution, would you mind checking the "Solved" button on my reply? It's easily overlooked if you don't know about it, but it can be found here, after clicking on the "…" button under a post:

And yes, as a relative newcomer to regex myself, I can absolutely vouch for their usefulness in all manner of scenarios :slight_smile:


A simpler, faster RegEx might be:

This will match all lines with nothing on them.

In the case of KM, we use (?m) to indicate multi-line.

For more info, see Regular Expressions (KM Wiki).

1 Like