Skip to main content
Inspiring
April 9, 2025
Answered

Process all open documents UXP

  • April 9, 2025
  • 1 reply
  • 858 views

uxp script
I would like to process all open documents
applying a brightness level of 20%
I tried with copilot to have a useful script
but I have no luck
this is the script I have now

async function apply20lum() {

        const documents = app.documents;
        if (!documents || documents.length === 0) {
            await alert("NO DOC OPEN.");
            return;
        }
        for (const doc of documents) {
            app.activeDocument = doc;
            const adjustmentLayer = await doc.createLayer({ kind: "brightnessContrast", name: "Luminosità 20%" });
            adjustmentLayer.brightnessContrast.brightness = 20;
        }

}
Correct answer jduncan

this is the script i put together

document.getElementById("btnPopulate").addEventListener("click", test1run);


async function test1() {


const docs = app.documents;

if (!docs || docs.length === 0) {
    app.showAlert("No active documents.");
    return;
}

for (const doc of docs) {
    app.showAlert(`processing ${doc.name}`);
    try {

      await test();
   
    } catch (error) {
       app.showAlert(error);
    }
}

}

async function test1run() {
  await require('photoshop').core.executeAsModal(test1, {
    commandName: 'Action Commands',
  })
}

async function bordo() {
    let result;
    let psAction = require("photoshop").action;

    let command = [
  
        {"_obj":"canvasSize","canvasExtensionColorType":{"_enum":"canvasExtensionColorType","_value":"foregroundColor"},"height":{"_unit":"distanceUnit","_value":142.0},"horizontal":{"_enum":"horizontalLocation","_value":"center"},"relative":true,"vertical":{"_enum":"verticalLocation","_value":"center"},"width":{"_unit":"distanceUnit","_value":142.0}}
    ];
    result = await psAction.batchPlay(command, {});
}

async function test() {
    await require("photoshop").core.executeAsModal(bordo, {"commandName": "Action Commands"});
}

 

 


This is because your batchplay only operates on the active document. In my example, I pass the variable `doc` to the function so the function can operate on each separate document. You are only looping over the documents and  logging their info, but not actually doing anything with the reference, so the bathplay is playing 4 times on the active document.

 

Instead, before you run the batchplay, update the active document and then run it. You should get the result you are after.

 

Also note, there may be a way to do this with `_target:[{_ref:"document", _id: 123}]` in your batchplay but I am not 100% sure.

 

const { app, core } = require("photoshop");
const { batchPlay } = require("photoshop").action;

async function doWork(doc) {
    return await core.executeAsModal(async () => {
        app.activeDocument = doc;
        let command = [
            {
                _obj: "canvasSize",
                canvasExtensionColorType: {
                    _enum: "canvasExtensionColorType",
                    _value: "foregroundColor",
                },
                height: { _unit: "distanceUnit", _value: 142.0 },
                horizontal: { _enum: "horizontalLocation", _value: "center" },
                relative: true,
                vertical: { _enum: "verticalLocation", _value: "center" },
                width: { _unit: "distanceUnit", _value: 142.0 },
            },
        ];
        return await batchPlay(command, {});
    });
}

const docs = app.documents;

if (!docs || docs.length === 0) {
    app.showAlert("No active documents.");
    return;
}

for (const doc of docs) {
    console.log(`processing ${doc.name}`);
    try {
        await doWork(doc);
    } catch (error) {
        console.error(error);
    }
}

 

1 reply

jduncan
Community Expert
Community Expert
April 9, 2025

This is how it shold work but I don't think most of the adjustment layer types have been implemented yet (docs: createLayer, LayerKind, PixelLayerCreateOptions). This code will give you an error `Error: Unknown kind brightnessContrast somehow passed validation.`. This does work with "NORMAL", "TEXT", and maybe one or two more, but I can't remember off-hand.

const { app, core, constants } = require("photoshop");

async function addLum20(doc) {
    return await core.executeAsModal(async () => {
        await doc.createLayer(constants.LayerKind.BRIGHTNESSCONTRAST, {
            name: "Luminosity 20% ",
        });
    });
}

const docs = app.documents;

if (!docs || docs.length === 0) {
    app.showAlert("No active documents.");
    return;
}

for (const doc of docs) {
    console.log(`processing ${doc.name}`);
    try {
        await addLum20(doc);
    } catch (error) {
        console.error(error);
    }
}

 Another potential solution is recording an action and executing that from within your script. You may be able to do something with batch play (check the Alchemist plugin) as well.

Inspiring
April 9, 2025

Thank you I'll try your solution tomorrow.