Activate Macro When KM 'Will' Terminate

I would like run a macro when the KM engine receives a 'will' quit signal, waiting for that macro to finish before KM actually quits. Any suggestions would be much appreciated.

Hey @JakeAce,

Welcome to the forum!  :sunglasses:

Please be more specific...

How is the quit being sent to Keyboard Maestro?

KM cannot intercept external quit events or the user activating quit from a menu item.


Thank you for your response. I'm executing a KM quit via AS in an app written in ObjC using NSTask. (ie. osascript -e 'tell application "Keyboard Maestro Engine" to quit'). I use the trigger "At engine launch" frequently. I would like to utilize the opposite "When engine will quit", and execute a parting macro. Suggestions?


Since you are sending the quit via AppleScript, precede that command with ones that fire off your clean-up macros via script triggers -- you might also want to include a delay tested against while executing to hold your quit until all macros have finished.

Another way would be to move your clean-up to the startup phase instead, so on launch you create your desired environment. It depends what you are trying to achieve with the clean-up.

1 Like

It seems to me that an osascript command could trigger a macro that first calls the required parting macro and then selects "Quit Engine" in Keyboard Maestro's file menu.

Might that be a risky approach for any reason, though?

It does over-complicate things -- OP's already using AppleScript, from which they can programmatically quit KM Engine, and if they go the menu route they should also add steps to determine if they had to launch the Editor to quit KME and so should quit that afterwards.

They could instead use the KM menu bar icon's menu item to quit the Engine if they have that enabled, but that introduces yet more complexity!

1 Like

Ah yes, of course. Thank you, I could tell I had missed something.

These approaches are good and basically how I've been doing things - using a helper app I wrote. Without a KM 'Execute when KM quits" trigger, I don't see another way to do parting tricks. But I may be missing something. Yes, it's a bit of work up-front. Including an 'Execute when KM quits" trigger would be a great addition to an already incredible utility. Perhaps it can be implemented in a future release of KM??


It's not about KM/KME 'intercepting' external app signals per se. It's about KM/KME implementing a trigger "Execute when engine quits" that utilizes the event 'applitationWillTerminate' within itself.

The idea is to have KME perform all macros with the assigned trigger "Execute when engine quits". Any macro triggered in this manner world be executed upon graceful exit of KME. (ie during logout, reboot, shut down or by the user quitting KME.) It's basically one last gasp of execution before terminating - hence the suggestion of "Execute when engine quits" for the name of the KME trigger event.

For reference:

Not knowing what language KM/KME was written in, I'm assuming Xcode (or like) is used for KM development. I utilize this event hook frequently within my own app development. The 'helper' app I wrote (as mentioned earlier) uses the 'applitationWillTerminate' hook. If KM/KME developers could implement such a trigger, it could supplant for requiring a user to 'write code' to perform such.

Please forgive me if this is n/a or otherwise presumptuous. I love KM!! I can only hope that this is received in that light.


You cannot do this. When Keyboard Maestro Engine is asked to quit, it will not wait around for macros to complete.

Instead of this, use AppleScript to execute a macro, and have the macro perform the actions and then have the macro quit the Keyboard Maestro Engine.

1 Like

Peter, thank you for all your hard work with KM. It is truly a wonderful tool!

I'm trying to keep from having another app running in the background just for a bail-out parachute. And manually running an AppleScript before/at bail-out, is exactly what I'm trying to avoid. I can only hope 'you cannot' is a temporary term.

In an effort to keep this topic alive... if KME were to execute a macro upon termination, that's not necessarily a bad thing. And if there are no "At engine quit" requests, KME would not "wait around for macros to complete". The 'applicationWillTerminate' event is totally transparent unless there's more stuff to execute. I guess I don't understand why this capacity is not already in the KME arsenal.

You're already sending the quit event via AppleScript. All you need to do instead is send a do script "Kill KME", where "Kill KME" is the name of a macro that does those cleanup things you want to do then quits the Engine. So no more work than you are already doing.

This makes things more flexible, not less, as you'd then easily be able to quit KME with or without cleanup, depending on your needs.

I doubt the vast majority of users quit KME unless troubleshooting (or aborting a runaway macro!), although I recall a thread from a year or so ago about restarting it to reclaim memory because of a leak in Apple's "typing" APIs. Out of interest, what's your use-case for this?

1 Like

Thanks for the idea.

I guess I did not realize KME could kill itself with an AppleScript macro. That opens up some possibilities.

One of the things I ran into before is Ventura always relaunching any app that was running at shutdown/reboot.

I'll have see if I can get things to work the way I want with this method and get back to my use-case once I've run this up the flagpole.

Thanks again for your interest.

Well, I'm still launching the helper app (I call it KMup) at login. It utilizes the 'applicationWillQuit' event and 'watches' 4 things: KME, WiFi, Stats & Clean My Mac X. If I use KMup to quit KME leaving KMup running, KME can be relaunched quietly during that session. If I use my KME reboot macro to bail & reboot, KME will then launch with the preferences KMup has set during its 'applicationWillQuit' event. Also, by asking KME to perform the restart via the Apple menuitem, I get Apple's graceful app quit(s) and a chance to abort rebooting.

BTW Many moons ago I used an app called QuicKeys - a predecessor of KM. It was retired around 2015 or so. It had a 'When QK quits' trigger that I used for this kind of thing. I still kinda miss it, but KM goes where QK could only dream of. Kudos to everyone at Stairways!

1 Like