How and Where to Add a Condition for an Error Message?

I have a macro that loads a tab-delimited glossary and uses it to tag source and target terms in my editor.

When a source term isn't present in the glossary, a cryptic message is displayed:

Screen Shot 2023-02-10 at 11.31.36

I can see that this message is triggered by the failing purple action:

Macro Image

Since I haven't caught any error conditions before, I would like to ask: How and where to add a condition for an error message that the source term isn't present in the glossary yet?

Tagging terms via glossary - Respecting word boundaries of target terms.kmmacros (35.7 KB)

This kind of "Search with Regular Expression" error happens because there's no match to the pattern you are searching for -- how you handle that depends on whether that matters!

If you are happy to have the capture group variable not be set -- so it retains its previously set value, or remains "empty" if not previously used -- you can simply turn off "Failure Aborts Macro" in the action's Options. In this example I've left "Notify on Failure" on so you can see it does indeed fail:

regex test skip error.kmmacros (3.4 KB)

Image

If you run it as-is you'll see that the search errors and Local__theWord retains its previous value. If you disable the first action so Local__theWord is never set you'll see it resolves to an empty string in the "Display" action.

So you could use this method -- either directly if you are happy to have an empty string, or by testing "If...Local__theWord is empty" and doing something based on that:

regex test empty error.kmmacros (4.5 KB)

Image

If you want something more "formal" you can use a "Try/Catch" block. Note that in this case you leave "Failure Aborts Macro" enabled and it's the "Try" block that catches the error and stops the macro from aborting -- you don't even get a notification:

regex test try:catch.kmmacros (4.7 KB)

Image

...and again you'll see that you get a "Failed!" dialog from the "Catch" but the macro continues to the end, with an empty string where the %Variable%Local__theWord% token is used.

As you can see, there's no "one size fits all" answer -- but there's plenty of options!

2 Likes

Thank you for you explanation.

I have solved my task like this:

All missing source terms are reported nicely. (Perhaps I'll add some actions to directly add them to the glossary later ...)

Before I posted my question I had search the wiki for 'catch' and 'error', but I didn't find this article: action:Try or Catch [Keyboard Maestro Wiki]

Maybe because I didn't search for 'failure'?

Anyway, perhaps your clear explanation can be the basis of a Wiki article, written from the user perspective (I want to catch an error, which action should I use?)?

1 Like

That's odd...

image

On second thought I must have searched for ‘error’ only.

1 Like

Hey Chris,

On third thought, I used 'error message':

https://wiki.keyboardmaestro.com/Home_Page?do=search&id=Home_Page&q=error+message

Background: I know a little VBA and there they call these failures 'errors' if I'm not mistaken.

Have good Sunday!

Hans

Hey Hans,

I see where you're coming from on this.

If you simply search the Wiki for "error" the hit list is big and hard to navigate. You can search for multiple criteria such as "error action", although this again creates a big hit list.

Unknown to most users is the Search Tools button with which you can further drill down to a more easily managed hit list:

Even so I've generally found that a Google site search more routinely finds what I want with less effort:

error action site:https://wiki.keyboardmaestro.com/ - Google Search

The very first hit when I tried it was the Try-Catch action.

I recommend writing a macro that makes doing a site-search on the Wiki simple, so you'll remember to use it.

I don't presently have one, because I have other tools that make it pretty simple, but I might change my mind and build a macro.

My search progression is usually:

  1. Search the Wiki with its own search tool.
    • I frequently find bread crumbs in the pop-up list the search field generates.

  1. Google Site-Search.

-Chris

2 Likes

I’ve never noticed that @ccstone even though I search the wiki a lot. This is a tip that’ll just save me even more time by reducing the endless lists of search hits I always seem to get😀 great advice re Google search too. Thanks!

1 Like