How can i use app.eventNotifier

Pleas I need an easier way to understand how to use this code:

app.eventNotifier = (event, descriptor) => { console.log(event, JSON.stringify(descriptor, null, ’ '));}

in real time. The documentation has not helped me.
Iwant to capture Events from Photoshop.

It doesn’t log anything for me (even though PS is in dev mode), so maybe Adobe removed this experimental feature.
You’re better of using event listeners, since they can be used in production, unlike app.eventNotifier.
If you want to capture all events, you could do

const eventHandler = (event, descriptor) => console.log(event, descriptor)
require("photoshop").action.addNotificationListener(['all'], eventHandler);

but you should rather only listen to the events you really need for your logic.
So let’s say you’d want to run some code whenever the user selects the layer called “Test Layer 1”,
you’d need to listen to the select event first of all. All the relevant information (or “instructions”) of the select event are stored in the descriptor, so you can use it to make further checks.

const onSelect = (_, descriptor) => {
  if(descriptor._target?.[0]._ref === "layer" && descriptor._target?.[0]._name === "Test Layer 1") {
    // -> The layer with name "Test Layer 1" was selected
  }
}

require("photoshop").action.addNotificationListener(['select'], onSelect);

If that descriptor._target?.[0]?. is confusing: If the event ran successfully, the reference to the target (receiving that action/event) is stored in _target. However, if something goes wrong that property might not be there, so it’s safer to use optional chaining to prevent any runtime errors.

2 Likes

As lots of times before, you’ve given me another tip - now how I can optimize my event listeners :slight_smile:

Nice :partying_face: What was the tip in particular?

That I don’t have to use one listener for all needed events and switch/case inside, but rather assign different functions for different events. I think this will make code more readable and maintainable

1 Like

That is good one. I wasn’t aware of that :smiley: But after checking my code I see I used it :smiley:

1 Like

I think listener for all events used for development was removed. So yes you have to listen “all” event but it will work only in development mode.

Thanks a lot.
I will test with the information.