title: Replacing smart objects in bulk with Photoshop
body: <p>Just facing this issue: I have a mockup in Photoshop with two smart-objects: Rectangle 14.psb and Place your logo.psb 
  I have 100+ images in png that should be applied to create mockups.
  For this reason, I would like your help to create a script that:</p>
  
  <p>Let me select the png file that I would like to use</p>
  
  <p>Open the smart objects (Rectangle 14.psb and Place your logo.psb)</p>
  
  <p>Re-Link the same png to the layers "place your logo" of both the smart objects.</p>
  
  <p>Finally, the script should save the file as png with the same file name of the selected png file adding just _new after its name.</p>
  
  <p>So far I have tried this code without any luck:</p>
  
  <pre><code>#target photoshop
  if (app.documents.length &gt; 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 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;*.png", true)
      } else {
          var theFiles = File.openDialog("please select files", getFiles, 
  true)
      };
      if (theFiles) {
          for (var m = 0; m &lt; theFiles.length; m++) {
              // Replace SmartObject
              theLayer = replaceContents(theFiles[m], theLayer);
              var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
              // 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|png|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
  };
  </code></pre>
  
  <p>The above code substitute the smart object but I would like just to re-link the layer withing the smartobject to a new image and save the file. Any help would be much appreciated!</p>