Do something when an app closes?

Is there a way to have an apple script wrap an application so that you could substitute the script for the applcation icon on the user end.
the script would change some property of the finder and then when you quit the application or switch to another application, the property would restore.
the instance i’m trying to tackle is hiding the dock when i start an arbitrary application, and then showing it when the app is done.
my problem is that i don’t understand how the application could communicate back to the script to tell it it has quit.
thanks,
ryan

In order for the app to communicate back to the script, the app needs to be ‘attachable’ - that is, it has the ability to call specific user-defines scripts at specific events. Unfortunately the number of attachable apps can probably be counted on one hand by a blind monkey.

About the only other way to do it is to have your script check periodically to see if the app is still running and react accordingly. This passive approach requires more resources (your script has to be running all the time, and using some CPU cycles each time it checks), but it’s about your only option.

Something like this (untested) should get you going:

on run
   <code here to hide the dock>
   tell application "some.app" to activate
end run

on idle
   tell application "System Events"
      set runningApps to name of every application process
      if runningApps does not contain "some.app'
         -- app is not running, so clean up
         <code to display dock>
         tell me to quit
     end if
   end tell
   return 10 -- check again in 10 seconds
end idle