Adding simple If - else logic and error trapping to macros


I am curious how to use If - else logic and error trapping with keyboard maestro macros?

Like this simplistic example...

If (

Safari app is active

Speak text "Safari is active"
Speak text "Safari is not active"

Is there some way to do this with KM

Like this?

There are other options in the dropdown under the application condition, like: is/is not running, is/not hidden.

You might also want to look at Switch/Case action


@evanfuchs has nicely covered conditional branching via If/Else and others, but if you do also want actual error trapping you should look at the Try/Catch action.


yes that is great - thanks very much evanfuchs!

Ok great I will also take a look at this - many thanks for the suggestion.

Good stuff - thanks.

If I could get the Computer Name (in sharing prefs) - this will be different. Then I can use if / then logic...

I am trying to use :

set var computerName to %MacName%

if computerName contains MyOfficeMac

then ... xxx

It seems to work for what I need ... Is this the best way?

It's simple and readable. Whether it's the best way depends on whether your computers' Sharing names ever change. If they do, or even might, then use the machine UUID instead: %MacUUID%.


Thanks for all the help.

I am curious if there is a way to test if a second monitor is attached?

Refer to this

1 Like

Thanks Much - I will check it out.

YES it works perfectly thanks.

1 Like