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

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.

:rolling_eyes:
ah. The pitfalls of having Discourse dump you in the middle of the thread and you weren’t sharp enough to figure it out. Thanks Dan. Sorry to trouble you over such minutiae.

Oh man, I can’t believe you caused me that much work. My fingers hate you! (Just kidding. :slight_smile:)

Despite an error in an "Execute Shell Script" action, the %ActionResult% token value continues to be set to "OK." I would like to implement an if statement that, in the case of an error, opens an actual terminal window and runs the command so that I can see and deal with the error.

The following images show the macro I am trying to implement and my Failure/Timeout settings on the action that I would like the %ActionResult% token to recognize as having failed.

Any ideas on how to get this to work?

Hey Markus,

Have a look at this thread:

Do Shell Script Grep Action Fails When Script Returns Nothing

-Chris