adobe-photoshop / generator-core

Core Node.js library for Adobe Photoshop CC's Generator extensibility layer
MIT License
692 stars 97 forks source link

Allow subscription to custom Photoshop "4-char" events #165

Open alekbarszczewski opened 10 years ago

alekbarszczewski commented 10 years ago
generator.onPhotoshopEvent('save',function(){ console.log('document saved'); });

Callback is never fired. Instead Generator prints warning:

Warning: unable to parse JavaScript message response 'save' as JSON. Error: SyntaxError: Unexpected token s`

But I guess event subscription works as this warning is called exactly when I save document. I know that save is not typical generator event but it would be cool to be able to subscribe to custom photoshop events though. Even if they don't return any data I think that callback should be fired. Or maybe I am doing something wrong?

joelrbrandt commented 10 years ago

I'm changing this to a feature request, because the onPhotoshopEvent function was never intended to support the "4-char" photoshop events you refer to. But, it probably could support those, and that's a good idea.

@alekbarszczewski If you're interested in capturing save events the "supported" way then you can do this by registering for imageChanged events. When a document is saved, you'll get an event that looks like this:

{
    "version": "1.1.0",
    "timeStamp": 1395148494.313,
    "count": 1,
    "id": 1116,
    "file": "/Users/jbrandt/Desktop/foo.psd",
    "metaDataOnly": true
}

Specifically, the file parameter will have changed, and the metaDataOnly flag will be set to true.

If there are other specific events you're hoping to register for, let me know, and I can weigh in as to whether there's a "supported" way to do that right now.

alekbarszczewski commented 10 years ago

As for save event there is also active parameter which means that document was activated - am I right? Because active parameter comes along with file and metaDataOnly parameters.

I don't need any specific "4-char" events right now, but it would be nice if they were supported. Thanks for answer.

joelrbrandt commented 10 years ago

@alekbarszczewski I'm not completely sure what your question is. But, if you want to know when the current (active) document changes, you can register for a separate network event: currentDocumentChanged

As an example, here's where our asset plugin does that: https://github.com/adobe-photoshop/generator-assets/blob/master/lib/documents.js#L458

But yes, I agree -- registering for any "4-char" event would be nice. As such, I'll keep this feature request open.

joelrbrandt commented 9 years ago

@timothynoel Might you be able to weigh in on how difficult it would be to allow Generator to register for the "4-char" events that ExtendScript can register for? In addition to @alekbarszczewski, the "Developer Support Team" here at Adobe has some customers requesting this.

timothynoel commented 9 years ago

The four char events use the same ExtendScript command (networkEventSubscribe) as imageChanged. You specify the event id in keventIDAttrStr (in place of generatorMenuChanged or imageChanged) The four char events don't return any JSON though when the events are fired, which may be causing the warning. If it's something beyond what's in the Generator SDK, you might need to consult the Photoshop SDK for the event ids. I haven't really done much Script listener or other Actions Plugin work, but that would be relevant I think.

JAndritsch commented 8 years ago

@joelrbrandt With regards to "imageChanged", I noticed that it fires in scenarios other than when a file is saved. For example, it fires whenever the active document changes, or when I make staged changes to the file without having saved it yet (such as a crop/rotate).

Is there any way to limit the scope of "imageChanged" to only fire when an actual save occurs? Or is there another event that I can listen to which will fire on save and provide me with JSON data about the saved file?

timothynoel commented 8 years ago

"imageChanged" is sent whenever the document is modified. The event comes with a json object describing what actually happened to the image. If you're just interested in file saves, look for a "file": entry in the json, (the value is the path of the file). There's more info in the wiki here: https://github.com/adobe-photoshop/generator-core/wiki/JSON-event-format

JAndritsch commented 8 years ago

Hey @timothynoel, thanks for your quick response.

I did see that I could rely on the presence (or lack) of a "file" attribute in the payload to determine whether I need to react to the event. The issue I am still having is that I see a "file" attribute in that payload even when navigating between open files in PS. It seems that "imageChanged" is firing on the same trigger that "currentDocumentChanged" fires. Is there any way to single out actual saves from tabbing between opened files (or opening new files)?

Thanks!

timothynoel commented 8 years ago

Okay, I understand. This may not be a perfect way to do it, but you could just filter out "imageChanged" with "active" : true, as that should only happen when the front document is switched

JAndritsch commented 8 years ago

It appears that they all come in as "active": true. For instance, if I switch between two opened documents, the one I switch to fires "imageChanged" with "active" set to true because it's now the active doc. The payloads look exactly the same on file open (or when I make it the active doc) when compared to a save.

timothynoel commented 8 years ago

When a file is saved, it should have "file":[pathname], but not "active":true (because that indicates that the document is becoming active, not that it is being saved). Or maybe there's still something I'm missing (?)