Reload Plugin Automatic when open a new Document

Question: It is possible that the plugin reloaded Automatically when opening a document or changing documents?

I found that layersFiltered event is called on document focus (basically when opened or changed), so I suppose you can add event listener for that and call simply window.location.reload() to reload your plugin

Thanks! let me try with this way.

May I ask what’s the use case? IMHO reloading if there’s nothing wrong, isn’t the best practice :thinking:

the case is: every time I open a new .psd document and press any button of the plugin it doesn’t work until I reload the plugin that is under development in order to work with that document.
And i think that the plugin should be actictve when that happens.

What do you mean by “the plugin it doesn’t work”? Do you get any specific error?
Maybe you’re assigning app.activeDocument to a variable globally instead of reading it at the moment of clicking a button?

I am using adobe uxp devolper tool to run the plugin. When I open a photo I have to go to uxp developer and press Reload if I don’t do that some functions when they are called by a button calling an event don’t work correctly. It is not because of the variables since the process before opening the new document is initialized by default once the process is finished. is see that when calling this method:

async function Layer_Create_with_Properties(new_Name, new_Opacity, new_Mode) {
const DocLayerNew = await DocLayer.createLayer({
name: new_Name,
opacity: new_Opacity,
mode: new_Mode,
doesn’t responde. But when press Reload in the Uxp Developer i works.
others controls that call others events happen the same.

So I think you should not reload whole plugin, but instead, call a function that you need.

P. S. Please use back-ticks (```)

    // to format your code

okay. After checking again I found that the problem is with some variables that are loaded in the header by calling the document properties. I know how to solve the problem.
Thanks for the suggestions.

I’m not sure I completely follow, but it sounds like you’re setting some variables once (at start) and then never setting them again.

You could fix this by just wrapping those steps in a function, and then you can call that function to reset the values whenever you want. (If you’re using batchPlay, you may need this to be an async function).

However, I’m going to suggest that, based on what you’ve describe so far, there’s a better way: load those values as close to the point in time that you need them as possible. In your function above, for example, perhaps:

const { app } = require("photoshop");
function async createNewLayer(name, {opacity = 100, mode = "normal"} = {}) {
    // we're not going to rely on a global -- we'll ask Ps what the
    // active document is _each time_ this function is called
    const activeDoc = app.activeDocument;
    return activeDoc.createLayer({name, opacity, mode});

/* later */
const newLayer = await createNewLayer("layer", {opacity: 50});

Now whenever you call createNewLayerWithProperties, it’ll always use the currently active document. You could actually go one step further and allow the function to take a specific document reference if you needed:

const { app } = require("photoshop");
function async createNewLayer(name, {opacity = 100, 
    mode = "normal", inDocument = app.activeDocument} = {}) {
    return inDocument.createLayer({name, opacity, mode});


const newLayerInCurrentDoc = await createNewLayer("test");
const NewLayerInFirstDocument = 
    await createNewLayer("test", {inDocument: app.documents[0]});

Now, you may have a complex batchPlay that you don’t want to repeat verbatim when you need it – in that case, wrap it in a function so that you can just call the function:

const {batchPlay} = require("photoshop").action;
async function getSomePsValue(/* any parameters you might have */) {
    return batchPlay(/* your batchPlay thing*/);

/* later */
async function doSomething() {
    const value = await getSomePsValue();
    /* do something with value */

If speed is an issue (some batch play commands might not be fast), then you can start thinking about caching values for performance, but I wouldn’t go there just yet. Start by moving the query for the value you need from Ps close to where you’re actually going to use it… and then you can see if you need to optimize.

Thanks for answering: the suggested solution is better coding for me.
I’ll take the suggestions. Thanks.
the case was; the variable get empty when changing the document,
so I thought to invoke it as you suggested when required in the event.

a solution would be sessionStorage or maybe localStorage if you want your data to stay there after Photoshop closes