Skip to main content
Known Participant
September 23, 2020
Question

script to batch smart object

  • September 23, 2020
  • 2 replies
  • 715 views

I have a mock-up template with sveral layers and a smart object where to put the image into this works perfect for one picture but i whant to do it on multiple pictures.

anyone who can help me with this? i have minimul script knowledge but found this script

 

// replace smart object’s content and save psd;

// 2011, 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;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// check if layer is smart object;

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", true)}

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

if (theFiles) {

// work through the array;

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

// replace smart object;

                    theLayer = replaceContents (theFiles, theLayer);

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

//Raise color picker for Back cover;

try {

app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];

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

var idsetd = charIDToTypeID( "setd" );

var desc7 = new ActionDescriptor();

var idnull = charIDToTypeID( "null" );

var ref2 = new ActionReference();

var idcontentLayer = stringIDToTypeID( "contentLayer" );

var idOrdn = charIDToTypeID( "Ordn" );

var idTrgt = charIDToTypeID( "Trgt" );

ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );

desc7.putReference( idnull, ref2 );

var idT = charIDToTypeID( "T   " );

var desc8 = new ActionDescriptor();

var idClr = charIDToTypeID( "Clr " );

var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

desc7.putObject( idT, idsolidColorLayer, desc8 );

executeAction( idsetd, desc7, DialogModes.ALL );

} catch (e) {};

//save jpg;

                    myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".psd")),psdOpts,true);

                    }

          }

}

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

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

          return true

          };

     };

////// replace 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

};

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
September 23, 2020

If your template have the smart objects layers that need their content populated on top of the layer stack  and the objects in the template are photoshop object like a psd. You should be able to use my BatchMockupTemplates.jsx its a script in my free download Photoshop Photo Collage and mockup Toolkit 

Either Replace Content or Edit Content can be used to update the Smart Object layers.

JJMack
cosmogateAuthor
Known Participant
September 25, 2020

Hi jjmack,

 

Thanks 4 your quick notice, i just downloaded your scripts and tried it on a mockup psd but got below errors.

--required folder missing c:\test\obj1

 

is it an idea i email you the files i have so you can test it or?

 

JJMack
Community Expert
Community Expert
September 25, 2020

You did not read the help. when a mockup the has more than a single Smart Object layer to be replaces you have your image collection is a folder in sub folders for each smart object layer the be replaced.  The Sub folder must be named obj0, obj1 , ... objN.   The top smart object layer will be populated with replacement in "obj0" folder.  The next lower layer with the image in "obj1" and so on.  The Script is simple It know nothing about  your images.  The images  are process in file name order.  Your need name your files so they will be populated together the first image in the folder are populated together.  You can use any naming convention that works for you.  Each sub folder is a queue for a layer in the layer stack.  the queues are processed in file order.

 

The script BatchReplaceOneObject.jsx was my prototype script is the only script the does not require  the sub folders for it can only replace the top smart object layer and only process one template

JJMack