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

PSD Artboards Script to change Smartobject and Save Artboards to Files

New Here ,
Apr 26, 2023 Apr 26, 2023

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 have a Photoshop file with 48 artboards, each with a different name.
  • Each artboard has a smart object layer called "Artwork" that's linked to eachother, normally if I change one, all of them changes. So I would like this script to take designs from a folder and run it by replacing it with a new image.
  • After replacing the smart object content, I want to export each artboard as a JPEG file with a quality of 5, and save it in a separate folders named after the design file, followed by the artboard name.

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




TOPICS
Actions and scripting , macOS

Views

1.6K

Translate

Translate

Report

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
Adobe
New Here ,
Apr 26, 2023 Apr 26, 2023

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

 

Votes

Translate

Translate

Report

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 26, 2023 Apr 26, 2023

Copy link to clipboard

Copied

What does the function replaceSmartObjectContent do with the argument »layer«? 

It does not seem to actually select the Layer. 

Votes

Translate

Translate

Report

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 27, 2023 Apr 27, 2023

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. 

Screenshot 2023-04-27 at 10.13.36.png

Votes

Translate

Translate

Report

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
New Here ,
May 01, 2023 May 01, 2023

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? 

Votes

Translate

Translate

Report

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 ,
May 01, 2023 May 01, 2023

Copy link to clipboard

Copied

LATEST
quotewhat you're saying is the proof we can't trust chatgbt.. 😞

Has anybody said one should or could trust it? 

 

quote

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. 

Votes

Translate

Translate

Report

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 26, 2023 Apr 26, 2023

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.

replace and export artboards to files.jpg

I will tag scripters to faster see this thread @c.pfaffenbichler @Stephen Marsh @jazz-y @r-bin 

Votes

Translate

Translate

Report

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
New Here ,
May 01, 2023 May 01, 2023

Copy link to clipboard

Copied

Actions would take forever, because there are 48 artboards in each PSD file..  I think.. maybe not? 

Votes

Translate

Translate

Report

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