Detect all running macros

Is there an action or script that will produce a list of the names of all running macros?

Is there a way for a macro to stop a specific other running macro?

Is there a way for one macro to tell another to quit (a) when the second was started asynchronously by the first, or (b) when the macros or entirely separate.

Is there a way to tell a macro to quit when a certain application quits?

I know how to cancel all running macros. I want to do this more selectively. In particular, I have a macro that starts an application and an asynchronous macro with a loop that interacts with the application each time around. I want to quit the asynchronous macro when the application quits, but I also want to be able to separately quite the asynchronous macro under other conditions before the application quits.

For the most part, no.

There is no way to get a list of running macros, and there is no way to specify or target a specific running instance.

Also, technically there is no such thing as a “running macro”. When a macro is triggered, its actions are set to the execution engine and then executed. So a macro can be changed after it is triggered and the change will have no affect on the running instance. Also a macro can be triggered multiple times to result in multiple running instances (of potentially varying versions of the macro). So while a “running macro” is terminology that is used regularly, it is quite imprecise which is fine most of the time, but for these sorts of questions the distinction becomes important.

So you cannot “tell a running macro” anything really. There is no way to target or specify a specific running instance.

On the other hand, a running instance itself can cancel itself whenever it decides. So the macro could contain checks to see if a variable has been set to 1, or if an application is not running, or if a time limit has expired or whatever, and then cancel itself.

So your specific case, I would add something like:

If variable “I Should Die” is 1 or application “Whatever is not running” then Cancel This Macro.

If necessary you can name that action “Check Death” and copy it to various places around your macro.

Note that if you have Pause Until actions, then you may need to change them into While or Until actions so you can include your Check Death action, or you can add the conditions to the Pause Until action.

Peter, doesn’t the KM Status Menu > Cancel Macros list all running macros?

From the KM Wiki:

You can cancel specific macros by selecting from the Cancel submenu in the Status Menu, or by selecting Start Debugging in the Status Menu, and clicking the appropriate cancel buttons.

Yes, but I was under the impression that @MitchellModel wants some sort of programmatic access to the list of running macros. The Cancel submenu in the Keyboard Maestro status menu does indeed list running macros and does allow you to cancel them, but it is not programatically accessible.

What about a little UI scripting?
I haven’t tried it – just a thought.

Yes, all of those questions for writing Scripts – AppleScript, shell script, KM script, or whatever combination it would take. I am particularly interested in stopping a particular asynchronous looping macro when an application it is monitoring quits. But it just occurred to me that I don’t need anything new for that – the loop can simply check if the application is running and quit if it’s not. The rest would be interesting, and I could think of things to do with them, but if they’re not feasible then forget about it.

1 Like

Hey, Were you able to find a solution for this?
I am pretty sure you must have worked your magic. :slight_smile:

Hey @forums2012,

Not possible – except through the status menu – and this cannot be scripted due to bugs in macOS' UI-Scripting model.

-Chris