How do I detect that the user has opened a new document?

Some of the variables retrieve data from the activeDocument object, so the plugin fails to properly initialize if it’s loaded up before any documents are open. What event can I listen for which would allow for those variables to get filled?

Standard listening for open events works most of the time (see Alchemist and other posts for info on listening). However, when a document is opened by Lightroom into Photoshop, no event is triggered. It’s a bug. You could set an interval timer (relatively slow to avoid performance issues) to check if the number of documents has changed as a workaround, which makes listening for the event unnecessary, but it would be ideal if Photoshop were fixed to emit an open event or something that indicates LR (such as a modal notification like we see for executeAsModal).

1 Like

If you check for number of documents… if you are fast enough you can open and close document but number of documents won’t change. Also I think that if action or plugin changes the active document then the event is not sent.

1 Like

Thanks for this advice! I suppose I could do something which periodically checks for open documents. I was afraid that I would have to have lots of duplicate code spread across multiple buttons, but it turns out that the activeDocument property I was accessing was only needed by one button, so I just put the initialization into that button’s callback function.

Would checking for the number of documents be something like accessing the
require('photoshop').app.documents array (even tho it’s actually an iterable proxy, which I’m still wrapping my head around) at regular time intervals?

Get require(‘photoshop’).app.documents.length to check for a change in the # open. Leaves open edge cases where another panel opened and closed and even number of documents, but probably not worth iterating and checking IDs. Should be sufficient.

1 Like