0 Replies Latest reply on Jul 20, 2017 8:56 AM by western18

    Batch replace different smart objects with different images in one file script

    western18

      Hi All,

       

       

      firstly sorry for my English, and thank you (The reader) for reading my question.

      So basically i am trying to find/combine scripts for weeks now but with my limited knowledge of scripting, i so far could not find a solution.

       

      I have a found script below which brings me so far the closest to what I try to accomplish.

      So i want to accomplish the following:

       

      I have a Template.psb

      inside that template i have 4 smart objects:

       

      Smart object Layer 1: Pillowcase_L

      Smart object Layer 2: Pillowcase_R

      Smart object Layer 3: Front_DHB

      Smart object Layer 4: Reverse_DHB

       

      I need a script that will replace the 4 smart objects based on folder's content and save that file as a .psd file with the name of the Smart object layer 3: Front_DHB to the folder called output.

       

      So here is a example:

       

      I have the folder: Template

      inside that folder i have the following files:

       

      Folder 1: Pillowcase_L

      Folder 2: Pillowcase_R

      Folder 3: Front_DHB

      Folder 4: Reverse_DHB

      Folder 5: Output

      Template.psd

       

      Inside Folder 1: Pillowcase_L  you have the following files:

      1.jpg or .tiff or .psd

      2.jpg or .tiff or .psd

      3.jpg or .tiff or .psd

      4.jpg or .tiff or .psd

      ..........

       

      Inside Folder 1: Pillowcase_R  you have the following files:

      1.jpg or .tiff or .psd

      2.jpg or .tiff or .psd

      3.jpg or .tiff or .psd

      4.jpg or .tiff or .psd

      ..........

       

      Inside Folder 1: Front_DHB  you have the following files:

      name_1.jpg or .tiff or .psd

      name_2.jpg or .tiff or .psd

      name_3.jpg or .tiff or .psd

      name_4.jpg or .tiff or .psd

      ..........

       

      Inside Folder 1: Reverse_DHB  you have the following files:

      1.jpg or .tiff or .psd

      2.jpg or .tiff or .psd

      3.jpg or .tiff or .psd

      4.jpg or .tiff or .psd

      ..........

       

      Folder 5: Output

      Empty for now

       

       

      When i run the script the following should happen:

       

      Smart object Layer 1: Pillowcase_L will be replaced with 1.jpg or .tiff or .psd from folder: Pillowcase_L.

      Smart object Layer 2: Pillowcase_R will be replaced with 1.jpg or .tiff or .psd from folder: Pillowcase_R.

      Smart object Layer 3: Front_DHB will be replaced with name_1.jpg or .tiff or .psd from folder: Front_DHB.

      Smart object Layer 4: Reverse_DHB will be replaced with 1.jpg or .tiff or .psd from folder: Reverse_DHB.

       

      The file will then be saved with it's layers intact in the folder: Output with the name: name_1.psd

       

      This will continue till all the images are processed It is important that the files lets say "1.jpg and name_1.jpg" are processed as one 1 file.

       

       

      So let's say i miss the file 1.jpg  in folder Pillowcase_L it then will not process like this:

       

      Smart object Layer 1: Pillowcase_L will be replaced with 2.jpg or .tiff or .psd from folder: Pillowcase_L.

      Smart object Layer 2: Pillowcase_R will be replaced with 1.jpg or .tiff or .psd from folder: Pillowcase_R.

      Smart object Layer 3: Front_DHB will be replaced with name_1.jpg or .tiff or .psd from folder: Front_DHB.

      Smart object Layer 4: Reverse_DHB will be replaced with 1.jpg or .tiff or .psd from folder: Reverse_DHB.

      Save

       

       

      It then should say could not start the script missing the following images:

       

      folder: Pillowcase_L:

      1.jpg or tiff or PSD

      4.jpg or tiff or PSD

      30.jpg or tiff or PSD

       

      folder: Pillowcase_R:

      None

       

      folder: Front_DHB:

      "...."_4.jpg or tiff or PSD

      "...."_35.jpg or tiff or PSD

       

      folder: Reverse_DHB:

      None

       

      Please add the missing files to the folder's and try again.

       

       

      I understand this is quite a request but if some one can help me with this it will save us a tremendous work as we have more then 5000+ to do and i am afraid we will not make the deadline T-T

       

      Again thank you who ever you are.

       

      Please see the script i have found so far:

       

      // 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;
          // 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", 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 + theName + ".psd")), psdOpts, true);
                  }
              }
          }
      };
      // 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
      };