Skip to main content
Participant
October 24, 2010
Question

Script that runs on "document close" event crashes when app is closed

  • October 24, 2010
  • 1 reply
  • 1840 views

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!

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
October 24, 2010

Did you try to using Try and Catch? I also do not think you can nest Script  events managers events. So a document close inside a script the was started by a document close event would not trigger an Event Managers document close event.

JJMack
Participant
October 28, 2010

I'm afraid that answer doesn't help. I'm not trying to nest scripts. The "close document" event is not being triggered from inside a script. It is triggered by the user closing the entire Photoshop app with an open document (Photoshop closes all open documents prior to the application closing). Try and catch don't work as no exception is raised inside the script.

Let me re-state the problem, hopefully making the situation a little more clear:

1) I run a script that lets me choose a sequence of images. It then enables itself (or rather a separate JSX file, but part of the same "tool") as a "Close Document" event handler.

2) When I close a document, this script automatically opens up the next image (Let's me work with files one on one in a sequence - for workflow when proofing a very large number of images).

3) If I suspend the sequence, it doesn't try loading anything when a document is closed. If I then close Photoshop, everything is fine. When I open Photoshop, I can resume the sequence. It then loads the next image in my list when I close an image.

4) If I do *not* suspend the sequence and I close Photoshop. My script still executes. I've added some logging to the script and see that no exception is ever raised. To the scripting runtime, everything proceeds properly - including the step tha tries to open the next document. BUT that next document never opens. Photoshop continues to close, and then shows me the crash message. It is very consistent. Every time my script is trying to open a document in response to the application closing, the app crashes and my script is never the wiser.

So what I'd like to do is to suspend/disable my script when the application is closing. If there is a way to programmatically detect that Photoshop is closing, that would be perfect. Or if there is a way to associate a separate script with a "Close Application" event, that should work, too (I could have that event handler, disable the other - or something similar to prevent the other from trying to open any documents.

Does that explain it any better?

Paul Riggott
Inspiring
October 28, 2010

The close event is after the close so you will not be able to anything. The only answer would be, do not use the close event but get the user to run the script for close and next document.