Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Process all open documents UXP

Enthusiast ,
Apr 09, 2025 Apr 09, 2025

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;
        }

}
TOPICS
Actions and scripting
102
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 10, 2025 Apr 10, 2025

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, ther

...
Translate
Adobe
Community Expert ,
Apr 09, 2025 Apr 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 09, 2025 Apr 09, 2025

Thank you I'll try your solution tomorrow.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 10, 2025 Apr 10, 2025

@jduncan 
unfortunately your script behaves exactly like copilot's,
it applies the effect only to the last image
it does not select the documents one by one.

maybe it's a bug?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2025 Apr 10, 2025

I think you misunderstood me. The `constants.LayerKind.BRIGHTNESSCONTRAST` layer kind isn't supported yet via the API (along with most other adjustment layer types). I was just showing you how you would go about scripting processing each open file (if the api did work). If you change the layer kind to `constants.LayerKind.NORMAL` and run my script you can see that the layer does get added to each document. See attached screen recording.

CleanShot 2025-04-10 at 11.50.35.gif

 Like I mentioned above, you could easily record an action that creates the adjustment layer and then execute that action from inside of your script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 10, 2025 Apr 10, 2025

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"});
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2025 Apr 10, 2025

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);
    }
}

 

CleanShot 2025-04-10 at 16.12.34.gif

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 11, 2025 Apr 11, 2025
LATEST

now i understand
thanks for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines