Applescript to alert user before closing any active application

Hi,

I’m new to the applescripting world. I’m always having trouble quiting applications that I didn’t mean to and I wanted to write a script that will alert me before closing any active application. I found on Google this applescript, but it doesn’t affect any application but itself. Can any of you guys help me with this?

on quit
	display dialog ¬
		"Really quit?" buttons {"No", "Quit"} default button "Quit"
	if the button returned of the result is "Quit" then
		continue quit
	end if
	-- Without the continue statement, the
	-- script application doesn't quit.
end quit

Thanks! :smiley:

Eduardo

Hi Eduardo,

detecting in any application, whether the user is going to quit the app, is rather impossible.
The quit handler of your example is a part of the script and affects only the script itself under certain circumstances.
You actually need something, which hooks globally into the ⌘Q shortcut and triggers the script.
This is only possible with a tool like QuicKeys, which can remap the shortcuts.
However changing the function of a menu item chosen with the mouse is not possible.

The script could be like this:

display dialog ¬
	"Really quit?" buttons {"No", "Quit"} default button "Quit"
if the button returned of the result is "Quit" then
	set frontApp to name of (info for (path to frontmost application))
	quit application frontApp
end if

Dear Stefan,

Thanks so much for your help! :smiley:

With your revised version of the applescript and Keyboard Maestro now everytime I hit “Option+Q” I get an alert message before I quit any application.

Very useful for me - believe me.

All the best,

Eduardo