Skip to main content
Participating Frequently
April 18, 2022
Question

Replacing Multiple Smart Objects via Script

  • April 18, 2022
  • 3 replies
  • 2237 views

I am using the following script, but would like to edit multiple smart objectives within my mockup template file. Basically my template file has a desk coaster and a mouse pad. The issue is that my image files will be different sizes. Is there a way to modify the script or another automated process to develop the mouse pad & coaster with the same image?

 

// Replace SmartObject’s Content and Save as JPG

// 2017, use it at your own risk

// Via @10354895 B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photoshops-variable-data-or-scripts/93359

// JPG code from here: https://forums.adobe.com/thread/737789

#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.embedColorProfile = true; 

    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

    jpgSaveOptions.matte = MatteType.NONE; 

    jpgSaveOptions.quality = 11;  

    // 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", "*.png;*.psd;*.tif;*.jpg", true)

        } else {

            var theFiles = File.openDialog("please select files", getFiles, true)

        };

        if (theFiles) {

            for (var m = 0; m < theFiles.length; m++) {

                // Replace SmartObject

                theLayer = replaceContents(theFiles[m], theLayer);

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

                // Save JPG

                myDocument.saveAs((new File(thePath + "/"  + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);

            }

        }

    }

};

// Get PSDs, TIFs and JPGs from files

function getFiles(theFile) {

    if (theFile.name.match(/\.(png|psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {

        return true

    };

};

// Replace SmartObject Contents

function replaceContents(newFile, theSO) {

    app.activeDocument.activeLayer = theSO;

    // =======================================================

    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID("null");

    desc3.putPath(idnull, new File(newFile));

    var idPgNm = charIDToTypeID("PgNm");

    desc3.putInteger(idPgNm, 1);

    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

    return app.activeDocument.activeLayer

};

//Alert Letting the User Know the Script Has Finished

alert("Finished!\nThe script has finished saving the jpeg thumbnails in the folder with the original mockup");

This topic has been closed for replies.

3 replies

Chuck Uebele
Community Expert
Community Expert
April 20, 2022

As far as getting the replaced smart object images to be the same size, as the original image, you can try the script on this link:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/paste-image-to-fit-smart-layer/m-p/12847605#M633018

c.pfaffenbichler
Community Expert
Community Expert
April 19, 2022

Please provide meaningful screenshots to illustrate the Layer and file-structure. 

Participating Frequently
April 20, 2022

Thanks for your assistance. See below

 

Participating Frequently
April 20, 2022

 

Stephen Marsh
Community Expert
Community Expert
April 19, 2022

Perhaps one of these scripts can help:

 

http://www.mouseprints.net/old/dpr/PhotoCollageToolkit.html

 

If you search the forum you will also find many other SO replacement scripts.

 

Good luck!

 

Participating Frequently
April 20, 2022

Hey @Stephen Marsh I searched the forums on replacing multiple SO scripts within one mockup. I have previously browsed and attempted to use the PHoto Collage Tool Kit pack, but was not successful. If you could point me to some threads on replacing multiple SO scripts I would be be very thankful! 

Stephen Marsh
Community Expert
Community Expert
April 20, 2022

@lido240674372bja wrote:

I have previously browsed and attempted to use the PHoto Collage Tool Kit pack, but was not successful. 


 

Why weren't you successful? Did you follow the 4 rules?

 

There are three scripts that may possibly be suitable:

 

BatchUpdateSmartObject.jsx - Used to populate simple Mockup templates like BatchReplaceOneObject script but can update more than a single smart object layer.


BatchMockupTemplates.jsx - Used to populate multiple Mockup Templates that needs to be populated with the same replacement image objects.


PopulateAlbumPageMockups.jsx - Used to populate a collection of Photo Page Mockup templates for a Picture Album.