简体   繁体   中英

How to detect the closing of a Photoshop document using CEP/JavaScript?

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:

var scriptPath = "/c/scripts/server.jsx";

//note the trailing space in "Cls "
var closeEventNotifierRef = app.notifiers.add("Cls ", File(scriptPath)); 

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:

var notifiersAll = app.notifiers;
var notifierRefs = [];

for (var i = 0; i < notifiersAll.length; i++){
    
    if  (notifiersAll[i].event == 'Cls ') {
        
        notifierRefs.push(notifiersAll[i]);
        
    }
    
}

for (var r = 0; r < notifierRefs.length; r++){
    
    notifierRefs[r].remove();
    
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM