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

Batch replace multiple smart objects in a single psd file

Community Beginner ,
Aug 11, 2023 Aug 11, 2023

Hi, I have been using a script to batch replace a single smart object layer and export to jpeg. It is working well but now I need to replace two smart objects in the same file and then export each iteration as a jpeg. Is there any way to update the code so it can replace one smart object with images from one folder, and the other smart object with images from another folder?

My coding knowledge is minimal so have no idea where to start, or if this is possible. All my images will be the same size if that makes any difference. Thanks!

TOPICS
Actions and scripting , Windows
1.1K
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 Beginner , Sep 29, 2023 Sep 29, 2023

If anyone is interested, I managed to get this sorted. Here is the script that will batch replace 3 smart objects within a PSD using files saved in 3 separate folders. The 3 smart objects need to be called 'Left' , 'Right', 'Middle' but I'm sure this can be easily edited for those with any coding knowledge. It also helps that the files in each folder all use the same file name system so that the images are paired up as you want. The script exports each iteration as a jpeg in the same folder loct

...
Translate
Adobe
Community Beginner ,
Aug 11, 2023 Aug 11, 2023

Here is the code I have been using to replace a single smart object layer:

// Replace SmartObject’s Content and Save as PSD
// 2017, use it at your own risk
#target photoshop
if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;
    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = 12;
    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
        if ($.os.search(/windows/i) != -1) {
            var theFiles = File.openDialog("please select files""*.psd;*.tif;*.jpg"true)
        } else {
            var theFiles = File.openDialog("please select files"getFilestrue)
        };
        if (theFiles) {
            for (var m = 0m < theFiles.lengthm++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
                // Save JPG
                myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptionstrue);
            }
        }
    }
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject Contents
function replaceContents(newFiletheSO) {
    app.activeDocument.activeLayer = theSO;
    // =======================================================
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnullnew File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm1);
    executeAction(idplacedLayerReplaceContentsdesc3DialogModes.NO);
    return app.activeDocument.activeLayer
};
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 Beginner ,
Sep 29, 2023 Sep 29, 2023
LATEST

If anyone is interested, I managed to get this sorted. Here is the script that will batch replace 3 smart objects within a PSD using files saved in 3 separate folders. The 3 smart objects need to be called 'Left' , 'Right', 'Middle' but I'm sure this can be easily edited for those with any coding knowledge. It also helps that the files in each folder all use the same file name system so that the images are paired up as you want. The script exports each iteration as a jpeg in the same folder loction as the PSD:

 

// Replace 'Left,' 'Middle,' and 'Right' Smart Objects' Content and Save as JPEG
#target photoshop

if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    
    // Find 'Left,' 'Middle,' and 'Right' layers
    var layer1 = findLayerByName("Left");
    var layer2 = findLayerByName("Middle");
    var layer3 = findLayerByName("Right");
    
    // JPG Options
    var jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = 12;

    // Check if all layers are found
    if (layer1 && layer2 && layer3) {
        // Select Files for 'Left' Smart Object
        var folder1 = Folder.selectDialog("Select folder for the 'Left' smart object images");
        if (folder1) {
            var files1 = folder1.getFiles(/\.(psd|tif|jpg)$/i);

            // Select Files for 'Middle' Smart Object
            var folder2 = Folder.selectDialog("Select folder for the 'Middle' smart object images");
            if (folder2) {
                var files2 = folder2.getFiles(/\.(psd|tif|jpg)$/i);

                // Select Files for 'Right' Smart Object
                var folder3 = Folder.selectDialog("Select folder for the 'Right' smart object images");
                if (folder3) {
                    var files3 = folder3.getFiles(/\.(psd|tif|jpg)$/i);

                    // Check if all folders have the same number of files
                    if (files1.length === files2.length && files1.length === files3.length) {
                        for (var i = 0i < files1.lengthi++) {
                            // Replace 'Left,' 'Middle,' and 'Right' Smart Objects
                            layer1 = replaceContents(files1[i], layer1);
                            layer2 = replaceContents(files2[i], layer2);
                            layer3 = replaceContents(files3[i], layer3);

                            var theNewName = files1[i].name.match(/(.*)\.[^\.]+$/)[1];

                            // Save as JPEG
                            myDocument.saveAs(new File(thePath + "/" + theName + "_" + theNewName + ".jpg"), jpgSaveOptionstrue);
                        }
                    } else {
                        alert("All folders should contain the same number of images.");
                    }
                }
            }
        }
    } else {
        alert("One or more of the 'Left,' 'Middle,' or 'Right' smart objects were not found in the document.");
    }
}

// Get PSDs, TIFs, and JPGs from files
function getFiles(theFile) {
    if (theFile.name.match(/\.(psd|tif|jpg)$/i) !== null || theFile.constructor.name === "Folder") {
        return true;
    }
}

// Find a layer by name
function findLayerByName(layerName) {
    var layers = app.activeDocument.layers;
    for (var i = 0i < layers.lengthi++) {
        if (layers[i].name === layerName) {
            return layers[i];
        }
    }
    return null// Layer not found
}

// Replace SmartObject Contents
function replaceContents(newFiletheSO) {
    app.activeDocument.activeLayer = theSO;
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnullnew File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm1);
    executeAction(idplacedLayerReplaceContentsdesc3DialogModes.NO);
    return app.activeDocument.activeLayer;
}
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