Copy link to clipboard
Copied
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;
}
}
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you I'll try your solution tomorrow.
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
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.
 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.
Copy link to clipboard
Copied
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"});
}
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
now i understand
thanks for your help.