Execute a script when quit any application

I need to execute a script when quit any application. How can I detect if the application has quitted?

Thanks.

Hi,

for example you can run this script (saved as stay open application) with an idle handler, which
checks (here every 5 seconds) whether the application is still running

on idle
	tell application "System Events" to set theApphasQuit to not (application process "theApp" exists)
	if theApphasQuit then
		-- 	do something
	end if
	return 5
end idle

Thanks EstefanK. it is what I need!!!