如何使用CEP/JavaScript检测Photoshop文档的关闭?

发布于 2025-02-06 05:13:38 字数 246 浏览 2 评论 0原文

我正在开发Photoshop上的扩展名,需要检测文档的关闭以将信息发送到服务器。 我的研究并没有引导我找到任何解决方案。

是否没有像ID这样的事件:

app.addEventListener('beforeClose', detectClose);

我唯一的解决方案是将打开的文档存储在数组中,并制作一个计时器,每x秒检查旧数组是否与新数组相同,但不是一个很好的解决方案。

先感谢您 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2025-02-13 05:13:39

确实,在Photoshop中,活动听众并不明显。 Photoshop中听众的最佳方法可能是使用Notifier(S)事件处理程序对象。请参阅 photoshop javascript参考以获取更多有关使用信息的信息。

通常,您使用文件>创建通知符;脚本>脚本事件管理器,您可以在其中选择“关闭文档” Photoshop事件,然后在发生时选择脚本或操作。

但是,如果您要创建扩展名并需要通过代码执行此操作,那就有些棘手。最好的选择是为关闭事件创建一个通知符:

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

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

添加该server.jsx文件中服务器需要发生的任何内容。请注意,只有在关闭文档后才调用该文件,因此我不确定仍然可以访问哪些信息。

还要注意,据我所知,添加通知器是持久的,这意味着每次都会每次都会发生密切的通知符和呼叫server.jsx。换句话说,一旦您的server.jsx脚本完成执行,将不会删除关闭事件通知器。

因此,您可能需要在服务器中添加逻辑。但是,这并不那么简单,因为您不再可以访问创建的Notifier参考(CloseEventNotifierRef)。我发现的唯一方法是循环浏览通知器:

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();
    
}

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文