What is a good way to do "if error then … else …"

I want to write a macro that is independent of a specify application, so I can’t use AppleScript. What is a good way to write a try/on error piece of a KM macro? I could package up the two sets of actions into a macro, then write an AppleScript that does the first and on error does the second, but I was hoping someone would have a KM-flavor suggestion.

1 Like

This may not be exactly what you're asking, but here's how I handle errors:

The "Cancel This Macro" causes action to go no further.

I actually have a macro that I use to build the Error Message and Cancel action. Its dialog looks like this:

Then depending on the options I choose, different Alert/Prompt actions are inserted, with or without sounds.

Interesting and helpful, but you’re right, not quite what I had in mind. I don’t want to cancel the macro, I want to take different actions if there is an error. Like an AppleScript try statement.

1 Like

I’d love to see a try/catch/finally, but it doesn’t exist.

The best you could do is put the actions in a sub-macro, and try to handle the errors when the sub-macro returns.

Are you guys aware of this token:
ActionResult Token

5 Likes

I am, yes.

1 Like

No, I wasn’t. Thanks. This will probably be sufficient. The weakness of that approach is that I can’t try a bunch of statements catching an error that happens in any of them — I have to test the result after every action that might fail. But it’s cool that it gives information about the failure.

So put all those actions in a submacro. Each one can exit the submacro.

If you don't care which action failed, you can do it like this:

Sub-Macro:
set SubMacroResult to "Error"
Do action.
if failed, cancel only this macro
Do the next action.
If failed, cancel only this macro
...
At the end, assuming successful, set SubMarcoResult to OK.

All the calling code as to do is:
Execute sub-macro.
Test SubMacroResult.

If you do care about which action failed, just change the above so that for each action that fails, set the SubMacroResult to whatever will tell you which action failed, before exiting the sub-macro.

And, if you want to do it without using any variables in the sub-macro, then pass the name of the result variable to TriggerValue, and use Set Variable to Text specifying %TriggerValue% as the name of the variable.

Using a sub-macro like this, witih multiple exit conditions, is a common programming paradigm.

Common programming paradigms are for sissies :smile:. Much more fun to invent your own.

LOL. :slight_smile:

It just keeps getting better and better doesn't it?

(\ __/)
(='.'=)
(⎺)_(⎺)

Somebody's got too much time on their hands. :smile:

It's an auto-text entry. Heh! Didn't take any time at all. :smile:

2 Likes

Many people working on KM macros have too much time on their hands — or think they do. :smirk:

1 Like

Not too sure about that, many of us use them to free up time, not to waste it.

1 Like

Didn’t say “waste” :-). The trick is to use KM to save more time then you spend working in KM. :slight_smile: :slight_smile:

Working on a macro is never a “waste” of time, sometimes it isn’t cost effective time wise though.

I think Group Action Timeouts might occasionally be useful in conjunction with this formulation.

1 Like

@DanThomas
Dan I am having trouble discerning what you mean because I can't decipher what you've used as a plain period "." and one that's possibly part of an action string command name. I can't tell if you intentionally left out spaces either, or you were typing on a Phone or something. Care to elaborate?

Look up further in this thread, and you’ll see my original message, with its proper formatting.