Any Way to Run a Script Before an Application Quits?

I tried using the Trigger 'Quits', but it looks like that fires after the application quits. I'm looking to gather a few things before the application exits.

Any hope?


How would Keyboard Maestro know when the application was going to quit?

Ha, question answered. I just wasn’t sure if there was possibly some sort of hook for a ‘before quit’ event available to us or something.

I’ll have to train myself to call my own script which does what I want and quits the app.


How do you quit it? If you quit it with Command-Q, you can have Keyboard Maestro have a Command-Q hot key enabled in the app, have it perform the task, and then File ➤ Quit the application.


Thanks, yes, I might try your Command-Q hot key idea… I’m suddenly realizing that I’m inconsistent in my closing of that app… sometimes keyboard, sometimes menu. I’ll have to work that out.

You could record the time the Command-Q hot key macro is triggered, and also trigger a macro when the app quits, and if the times are not very close, alert you that you have failed to run the first macro.

Does Keyboard Maestro capture (and thus act on) a ⌘Q event before the app does?

Yes, it should.

In general, you can trap app shortcuts using a KM Hotkey trigger.
I believe Hotkeys are evaluated before keyboard shortcuts.


I was looking for a way to do this, and I see the logic of the answer, but how does one get Keyboard Maestro to "have a command-Q hot key enabled in the app", so that one can then have it perform some task or series of tasks before one then gets it to quit?

In other words, I think I'm asking: "how do I set a trigger that equates to when command-Q is pressed in only one specific application?" (if I have the logic of your answer right).

I may be answering my own question, but I think I have it: I set up a simple trigger for command-Q, but include in the "If All Conditions Met Execute Actions" a condition "if the application [in question] is at the front". I hope I have that right.

ha! clever

I think a common way of doing this is to create a macro “Group” (the far left column in Keyboard Maestro Editor) and have that group defined to only be active in a specific application.

Any macros saved to that group will then, by definition, only be triggered when you are using that app, so you could remap ⌘Q without worrying about it affecting other apps.

1 Like

Ah - that's a good idea.

I had, instead, just added on to the end of the macro - "otherwise execute the following actions - Quit - front application".

But your method seems more precise, since some of the triggers using this general method might have different consequences in different apps.

Thank you.

Well, if I place the macro in a group that's defined as "Available in these applications" or as "Available when these applications are running", and I only specify the target application, the command-q stops working in that specified app, but continues to work for all others.

This must be because the conditional logic requires the macro to be run when one app is at the front and another is running.

So, I re-introduced the "otherwise execute the following actions" - "Quit Front Application".

When we use CMD + Q as a hot key trigger, it will no longer quit the app as it used to. Instead, it tells KM to execute the macro that uses CMD + Q as a trigger. Therefore, we need to tell KM to quit the app in this macro.


Without a macro or screenshot for anyone to evaluate, note you have other options. Something like this would also work:


Just posting this related tidbit here in case it helps anyone else... I always had an issue with accidentally hitting the Q key when I meant to press CMD-W to close, not quit, a window or tab. This happened to me a lot in Firefox. So, I used KM to create a macro that quits the front application on CMD-Q Double Tap. Now CMD-Q on its own does nothing and I never accidentally quit things anymore, but quitting them is as easy as a double-tap when needed. It's a simple but elegant solution for me.

1 Like

Yup, some apps don't have any confirmation, so I made one to intercept Cmd-W, and one for Cmd-Q.
Works wonders

1 Like