Application Trigger: Distinguish between Activate and Launch

I am stumped. Someone probably solved this problem, but I can not find an answer on these forums. I want to detect the difference between an application Launching and Activating. My macro needs to do different things when it Launches than when it Activates. The problem I'm having is that when an application Launches, the Activate trigger is also "true" - so the Macro using Activate as a trigger is run and I don't want it to.
My current workaround is as follows: when the application Launches, an action is the macro will fail. So I test for the failure of that action (with the ActionResult token); when a failure occurs, I know the application has Launched, and the macro can proceed accordingly. While this works for this macro, it doesn't seem very elegant. I'm thinking there probably is a method to distinguish between Launching and Activating. Thanks in advance for any help.

I have to admit that I am surprised by this behavior, which I just confirmed.
So, to be clear, a Macro triggered by App Activation will trigger when the app is launched as well as when it is already running, but is just made the active app (frontmost).

Your logic may be just as good, or better, than mine, but here is the solution I came up with.

updated 2018-06-11 21:30 CT

My original macro logic was flawed. I think this should fix it.
You will need two Macros:

  1. Triggered when app launches: Sets KM Variable DND__AppJustLaunched to "YES"
    • Does whatever processing on Launch that you want.
    • Should be in a Global Macro Group
  2. Triggered when app activates:
    • But also when app launches
    • Should be in a Macro Group available ONLY when app is active.
    • Checks the KM Variable DND__AppJustLaunched to see if it is "YES"
    • IF so, then does nothing except set KM Variable DND__AppJustLaunched to "NO"
    • Else, does whatever processing on Activate that you want.

Please give these a test to see if it works for you.

Note: These test macros are setup to use BBEdit.

You will need to change them for whatever app you are using.
The Macro Group for Macro #2 will also be for this same app.

MACRO:   TEST Launch of BBEdit

Must be in a GLOBAL Macro Group

~~~ VER: 1.1    2018-06-11 ~~~


TEST Launch of BBEdit.kmmacros (2.4 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


MACRO:   Test for App Activates Trigger

Must be in a Macro Group Available ONLY When App is Active

~~~ VER: 1.2    2018-06-11 ~~~


Test for App Activates Trigger.kmmacros (8.2 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.



You had a clever idea. As you've discovered, the flaw is that your DND_AppJustLaunched variable never gets reset. One solution would be to create a macro that runs when the app in question quits; that macro would set the value of DND_AppJustLaunched to Yes. A potential bug is that when the application crashes, the macro (which is intended to run when it quits) probably won't run - I have yet to test this.
I'll keep thinking about it. Thanks much for posting. Please let me know if you come up with anything.

Just posted an update to my solution. Hope this works for you.
It has worked for me in limited testing.

JMichaelTX: Thank you for another clever solution. I was not aware that Macro Groups could be restricted to only being available when an application is running (I'm new to Keyboard Maestro). Given that, the macros I want can be constructed without the need for the DND_AppJustLaunched variable. I can achieve the effect of differentiating between Launch and Activate: The macro that should run at Launch is in a group that is always available. The macro that should run at Activation but not Launch is in a group that is only available when the app in question is running.
Thank you very much for taking the time to respond and create your macros. I learned quite a bit.

Perhaps it will work differently for you, but when I tested it this macro was also triggered when the app was launched.

You are correct - there was a bug in my testing. Finding a bug in the method you use to test for bugs reminds me of a quote from a great novel, "Tinker, Taylor, Soldier, Spy" - which is "who spies on the spies?"