title: Photoshop javascript batch replace smart layer from folder and resize
body: <p>I am trying to work out how to use javascript with photoshop, but eventhough i dont find a logical error in the code, it doesnt work properly.</p>
  
  <p>I have a folder of 1000+ images/.ai files that have varying dimensions. I need these images on the Pillow and saved as .jpeg.
  I choose the smartlayer and run the script to choose the images and it saves them correctly. The only problem is, that the resizing of images and positioning dont work properly.
  If i put the image in manually, it works without issues, but not with the script.</p>
  
  <p>If the width is greater than the height, it should set the width to 1200 px and calculate the height according to that. (and vice versa) and place in the middle of the layer.</p>
  
  <ol>
  <li>How do i fix the resizing and positioning?</li>
  <li>Is it possible to choose a folder where the images are inside instead of selecting the images?</li>
  <li>How do i handle it when there are 2 smart layers to change in the mockup instead of 1?</li>
  </ol>
  
  <p>Anyone know where the problem lies this code?
  Im grateful for any bit of help!</p>
  
  <pre><code> // Replace SmartObject’s Content and Save as JPG
  // 2017, use it at your own risk
  // Via @Circle 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 &gt; 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 = 8;   
      // 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;*.ai", true)
          } else {
              var theFiles = File.openDialog("please select files", getFiles, true)
          };
  };
  (function (){
      var startRulerUnits = app.preferences.rulerUnits;  
      app.preferences.rulerUnits = Units.PIXELS;  
      var bounds = activeDocument.activeLayer.bounds;  
      var height = bounds[3].value - bounds[1].value;
      var width = bounds[2].value - bounds[0].value;
  if (height &gt; width){ 
      var newSize1 = (100 / width) * 800;  
      activeDocument.activeLayer.resize(newSize1, newSize1, AnchorPosition.MIDDLECENTER);
      app.preferences.rulerUnits = startRulerUnits;  
      }  
  else{
      var newSize2 = (100 / height) * 800;  
      activeDocument.activeLayer.resize(newSize2, newSize2, AnchorPosition.MIDDLECENTER);
      app.preferences.rulerUnits = startRulerUnits;      
      } 
  })();
          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 + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
              }
          }
      };
  // 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(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>I have attached 2 pictures. 1 how it needs to look like and 2 what the script outputs
  <a href="https://i.stack.imgur.com/Qufui.jpg" rel="nofollow noreferrer">Correct</a>
  <a href="https://i.stack.imgur.com/9lHTM.jpg" rel="nofollow noreferrer">Wrong</a></p>