Script that runs on "document close" event crashes when app is closed
I've written a script that runs when a document closes. It actually opens another document. (A main UI script lets you define a sequence of images to work with and it then loads them one after the other using the "document close" event to trigger the next item in the list)
If I have an image open and then close the whole application, the script still executes, but Photoshop suddenly crashes (I get the Windows 7 dialog asking if I want to report the error...). I look at the contents of the data file that is maintained by the scripts and see that the script tried to open the next document and received no exceptions (even though the next image obviously did not open - but seems like the likely culprit to the application crashing).
Is there a way to detect that the application is closing and immediately exit from this script? Or is there a way to hook into an application closing event (which can then set settings in the data file to prevent the "document close" event script from trying to open the next item)? Ideally the script wouldn't even bother executing (or, if executing, have the logic to skip its behavior) when the whole app is closing. Is there a way to implement this?
I'm using Photoshop CS4 but I need solution that will work with CS5, too. If it can only be done with CS5, that information would be useful. I'm using 64-bit Windows 7, but I need a solution that is not platform-specific (as I plan to run the script on a Mac as well).
The language I'm using for the script is JavaScript.
Thanks for any guidance and assistance!