I'm developing an extension on Photoshop and I need to detect the closing of a document to send information to the server. My research did not lead me to any solution.
Is there not an event like on ID like :
app.addEventListener('beforeClose', detectClose);
The only solution I have is to store the open documents in an array and make a timer that every x seconds checks if the old array is the same as the new one but it's not a great solution.
Thank you in advance !
Indeed, event listeners are not obvious in Photoshop. Probably the best approach for listeners in Photoshop is to make use of the Notifier(s) event-handler object. See the Photoshop Javascript Reference for more information on usage.
Typically, you create Notifiers using File > Scripts > Scripts Event Manager, where you can choose the "Close Document" Photoshop event and choose a script or an action to run when it happens.
But, if you are creating an extension and need to do this via code, it's a little trickier. Your best bet is to create a Notifier for the close event:
Add whatever needs to happen with the server in that server.jsx file. Note that the file will only be called after the document is closed, so I am not sure what info will still be accessible.
Also note that, as far as I know, adding notifiers is persistent, meaning that close notifier and the calling of server.jsx will happen every time after. In other words, the close event notifier will not be deleted once your server.jsx script finishes executing.
Because of this, you may want to add logic inside your server.jsx file to take care of removing the close event notifier at the end. However, this is not as simple, since you no longer have access to the notifier reference created (closeEventNotifierRef). The only way I found was to loop through notifiers:
Hope this helps.