Copy link to clipboard
Copied
Hi everyone,
I'm trying to automate a task in Photoshop using a script, but I'm having trouble getting it to work as expected. Here's what I'm trying to achieve:
I found a script online that almost does what I need, but it's not working as expected. It's giving me an error message "No artboard named found in the active document". I also tried another script that lists all artboards, but it's not showing any artboards in my document.
Can someone help me modify the script to achieve my goal? I'm not very experienced with scripting, so any help would be greatly appreciated.
Thank you!
This is the original video I found the info: https://www.youtube.com/watch?v=-t8zcTfiG7c&ab_channel=Alek
The original script: https://drive.google.com/file/d/1fLKSJHkUp4TRGBQ6qL4f7PBbPH__cUcv/view
And this is the script edited with the help of chatgbt 🙈:
#target photoshop
function main() {
if (app.documents.length === 0) {
alert('There are no documents open.');
return;
}
var doc = app.activeDocument;
var layer = findSmartObjectLayer(doc.layers);
if (!layer) {
alert('No smart object layer found in the active document.');
return;
}
var smartObjectName = layer.name;
var folder = Folder.selectDialog("Choose a folder to save the images");
if (!folder) return;
var artboards = getArtboards(doc);
if (artboards.length === 0) {
alert('No artboards found in the active document.');
return;
}
for (var i = 0; i < artboards.length; i++) {
var artboard = artboards[i];
doc.activeLayer = layer;
replaceSmartObjectContent(layer, artboard);
saveArtboardAsJPEG(doc, artboard, folder);
}
alert('Artboards exported successfully.');
}
function findSmartObjectLayer(layers) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
if (layer.typename === 'ArtLayer' && layer.kind === LayerKind.SMARTOBJECT) {
return layer;
} else if (layer.typename === 'LayerSet') {
var result = findSmartObjectLayer(layer.layers);
if (result) return result;
}
}
return null;
}
function replaceSmartObjectContent(layer, artboard) {
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
desc3.putPath(charIDToTypeID("null"), new File(artboard));
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
}
function getArtboards(doc) {
var artboards = [];
for (var i = 0; i < doc.layerSets.length; i++) {
if (doc.layerSets[i].artboardEnabled) {
artboards.push(doc.layerSets[i]);
}
}
return artboards;
}
function saveArtboardAsJPEG(doc, artboard, folder) {
var artboardRect = artboard.bounds;
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.JPEG;
exportOptions.quality = 5;
var fileName = doc.name.replace(/\.[^\.]+$/, '') + '_' + artboard.name + '.jpg';
var destination = new File(folder + '/' + fileName);
doc.exportDocument(new Rect(artboardRect[0], artboardRect[1], artboardRect[2], artboardRect[3]), ExportType.SAVEFORWEB, exportOptions);
}
main();
Copy link to clipboard
Copied
Then I said, let's only change the first artboards smartobject, and entered it's name, hoping it would find it...
Any idea??
#target photoshop
function main() {
if (app.documents.length === 0) {
alert('There are no documents open.');
return;
}
var doc = app.activeDocument;
var artboard = findArtboard(doc.layerSets, "FSEMPVS semi transparent vellum invitation silver foil pressed");
if (!artboard) {
alert('No artboard named "FSEMPVS semi transparent vellum invitation silver foil pressed" found in the active document.');
return;
}
var layer = findSmartObjectLayer(artboard.layers, "Artwork");
if (!layer) {
alert('No smart object layer named "Artwork" found in the specified artboard.');
return;
}
var folder = Folder.selectDialog("Choose a folder to save the images");
if (!folder) return;
var artboards = getArtboards(doc);
if (artboards.length === 0) {
alert('No artboards found in the active document.');
return;
}
replaceSmartObjectContent(layer, artboard);
for (var i = 0; i < artboards.length; i++) {
saveArtboardAsJPEG(doc, artboards[i], folder);
}
alert('Artboards exported successfully.');
}
function findArtboard(layerSets, targetName) {
for (var i = 0; i < layerSets.length; i++) {
var layerSet = layerSets[i];
if (layerSet.artboardEnabled && layerSet.name === targetName) {
return layerSet;
}
}
return null;
}
function findSmartObjectLayer(layers, targetName) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
if (layer.typename === 'ArtLayer' && layer.kind === LayerKind.SMARTOBJECT && layer.name === targetName) {
return layer;
} else if (layer.typename === 'LayerSet') {
var result = findSmartObjectLayer(layer.layers, targetName);
if (result) return result;
}
}
return null;
}
function replaceSmartObjectContent(layer, artboard) {
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
desc3.putPath(charIDToTypeID("null"), new File(artboard));
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
}
function getArtboards(doc) {
var artboards = [];
for (var i = 0; i < doc.layerSets.length; i++) {
if (doc.layerSets[i].artboardEnabled) {
artboards.push(doc.layerSets[i]);
}
}
return artboards;
}
function saveArtboardAsJPEG(doc, artboard, folder) {
var artboardRect = artboard.bounds;
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.JPEG;
exportOptions.quality = 5;
var fileName = doc.name.replace(/\.[^\.]+$/, '') + '_' + artboard.name
Copy link to clipboard
Copied
What does the function replaceSmartObjectContent do with the argument »layer«?
It does not seem to actually select the Layer.
Copy link to clipboard
Copied
I looked a bit more at the Script from post 1 (not from the original post) and it appears to contain quite a bit of problems.
• Why "===" instead of "=="?
• What is "artboardEnabled" supposed to be in DOM-code?
• Where are the replacement files for the Smart Object defined or do you want to do that manually each time? The line "replaceSmartObjectContent(layer, artboard);" would try to feed the artboard into the Smart Object instead of a file.
• Do you know what "exportDocument" does? Because feeding it "new Rect(artboardRect[0], artboardRect[1], artboardRect[2], artboardRect[3])" as the first argument does not seem to make sense.
Copy link to clipboard
Copied
well..... I'm not familiar with scripting or code..
what you're saying is the proof we can't trust chatgbt.. 😞
and I should give up on creating a script for artboards file on PSD?
Copy link to clipboard
Copied
what you're saying is the proof we can't trust chatgbt.. 😞
Has anybody said one should or could trust it?
and I should give up on creating a script for artboards file on PSD?
You could familiarize yourself with Photoshop Scripting.
Though at current it might be more prudent to go to UXP Panels directly instead of learning Scripting that may become obsolete in a while.
Copy link to clipboard
Copied
I am not scripting guy. Can you further explain your automation needs because this can be done using action, for example.
I will tag scripters to faster see this thread @c.pfaffenbichler @Stephen Marsh @jazz-y @r-bin
Copy link to clipboard
Copied
Actions would take forever, because there are 48 artboards in each PSD file.. I think.. maybe not?