4 Replies Latest reply on Mar 13, 2015 11:25 AM by mikel28071654

    Batch sequence from 2 folders

    mikel28071654 Level 1

      Hello,

       

      I would like to create a code, where I have a sequence of files in a Left folder and a sequence of files in a Right folder. The goal is to open the left image (L_001) and place the right image (R_001) on top, by creating a new layer. Bottom layer to be called, 'Left' and top layer to be called, 'Right'. Then save the two layered image as a PSD. Then to carry on, L & R _002, 003, so on.

       

      Can you advise me on a code to perform this?

       

      Thanks!

        • 2. Re: Batch sequence from 2 folders
          mikel28071654 Level 1

          #target photoshop

          // dialog for folder-selection;

          var theFolderOne = Folder.selectDialog ("Choose the correct LEFT folder of images");

          var theFolderTwo = Folder.selectDialog ("Choose the correct RIGHT folder of images");

          var theFolderThree = Folder.selectDialog ("Choose the correct MERGE folder of images");

          if (theFolderOne && theFolderTwo && theFolderThree) {

          var theFilesOne = theFolderOne.getFiles(checkFor);

          var theFilesTwo = theFolderTwo.getFiles(checkFor);

          // check if both folders contain the same number of files;

          if (theFilesOne.length != theFilesTwo.length) {

          alert ("the folders don’t contain the same number of images")

          }

          // else do the stuff;

          else {

          // create the psd-options;

          psdOpts = new PhotoshopSaveOptions();

          psdOpts.embedColorProfile = true;

          psdOpts.alphaChannels = false;

          psdOpts.layers = true;

          psdOpts.spotColors = true;

          // run through the files;

          for (var a  = 0; a < theFilesOne.length; a++) {

          // open background-image;

          var theFile = app.open(File(theFilesOne[a]));

          theFile.activeLayer = theFile.layers[0];

          // place foreground-image;

          var idPlc = charIDToTypeID( "Plc " );

          var desc6 = new ActionDescriptor();

          var idAs = charIDToTypeID( "As  " );

          var desc7 = new ActionDescriptor();

          var idfsel = charIDToTypeID( "fsel" );

          var idpdfSelection = stringIDToTypeID( "pdfSelection" );

          var idpage = stringIDToTypeID( "page" );

          desc7.putEnumerated( idfsel, idpdfSelection, idpage );

          var idPgNm = charIDToTypeID( "PgNm" );

          desc7.putInteger( idPgNm, 1 );

          var idCrop = charIDToTypeID( "Crop" );

          var idcropTo = stringIDToTypeID( "cropTo" );

          var idboundingBox = stringIDToTypeID( "boundingBox" );

          desc7.putEnumerated( idCrop, idcropTo, idboundingBox );

          var idPDFG = charIDToTypeID( "PDFG" );

          desc6.putObject( idAs, idPDFG, desc7 );

          var idnull = charIDToTypeID( "null" );

          desc6.putPath( idnull, new File( theFilesTwo[a] ) );

          var idFTcs = charIDToTypeID( "FTcs" );

          var idQCSt = charIDToTypeID( "QCSt" );

          var idQcsa = charIDToTypeID( "Qcsa" );

          desc6.putEnumerated( idFTcs, idQCSt, idQcsa );

          var idOfst = charIDToTypeID( "Ofst" );

          var desc8 = new ActionDescriptor();

          var idHrzn = charIDToTypeID( "Hrzn" );

          var idRlt = charIDToTypeID( "#Rlt" );

          desc8.putUnitDouble( idHrzn, idRlt, 0.000000 );

          var idVrtc = charIDToTypeID( "Vrtc" );

          var idRlt = charIDToTypeID( "#Rlt" );

          desc8.putUnitDouble( idVrtc, idRlt, 0.000000 );

          var idOfst = charIDToTypeID( "Ofst" );

          desc6.putObject( idOfst, idOfst, desc8 );

          var idAntA = charIDToTypeID( "AntA" );

          desc6.putBoolean( idAntA, true );

          executeAction( idPlc, desc6, DialogModes.NO );

          // save the combined files;

          theFile.saveAs(new File (theFolderThree + "/LRMerge" + bufferNumberWithZeros((a + 1), 3) ), psdOpts)

          theFile.close(SaveOptions.DONOTSAVECHANGES)

          }

          }

          };

          ////////////////////////////////////

          ////// check for psd, tif or jpg //////

          function checkFor (theFile) {

          if (theFile.name.slice(-4) == ".psd" || theFile.name.slice(-4) == ".tif" || theFile.name.slice(-4) == ".jpg") {return true}

          else {return false}

          };

          ////// buffer number with zeros //////

          function bufferNumberWithZeros (number, places) {

          var theNumberString = String(number);

          for (var o = 0; o < (places - String(number).length); o++) {

          theNumberString = String("0" + theNumberString)

          };

          return theNumberString

          };

           

          I have adjusted this code in terms of folder name in the first few folders, but what I would like to do now is select the background layer and rename that to 'Left' and then the layer on top 'R_xxx' to 'Right'. Not so great at coding, but would appreciate if someone could implement the request into the code above please?

          • 3. Re: Batch sequence from 2 folders
            JJMack Most Valuable Participant

            When new layers are added they are the active layer  so as you add the layers name them to what you want. It looks like the first is opened if it is a flat file like a jpeg the layer will be a background layer  if the file is a layered document it would need to be flattened to make a single layer it would be a background layer if the file was a pnn the layer would be a normal layer but might contain transparency.   The Script could also increase the canvas size an move move the images side be side.

            It the two images are not the same size the script could scale them to a common height etc.  Only you know the details of your files.  With that knowledge you can design the process you want to automate. A background layer may need to be converted to a normal layer to rename it.

             

            app.activeDocument.activeLayer.name="Left";

            • 4. Re: Batch sequence from 2 folders
              mikel28071654 Level 1

              Thank you for your detailed explanation JJMack. I have implemented it via PhotoShop using an action (JR_3D_2). When I run this script it looks for those files made from the previous comments and performs the action I have created. It should then save as JPEGs with the options I have typed. When I run this script, it loads the image in, performs the action and then it wants to save. It opens the save dialogue box, even though I have provided the options and file name...

               

              ////////////////////////////////

              ////////Open the Merge folder////

              var theFolderMerge = Folder.selectDialog ("Choose the correct MERGE folder of images");

              var theFilesOne = theFolderMerge.getFiles(checkFor);

              // create the jpg-options;

              jpgOpts = new JPEGSaveOptions();

              jpgOpts.embedColorProfile = true;

              jpgOpts.formatOptions = FormatOptions.PROGRESSIVE;

              jpgOpts.quality = 12;

              jpgOpts.scans = 5;

              // run through the files;

              for (var a  = 0; a < theFilesOne.length; a++) {

              // open background-image;

              var theFile = app.open(File(theFilesOne[a]));

              theFile.activeLayer = theFile.layers[0];

              //action

              doAction ('JR_3D_2','Default Actions',DialogModes.NO);

              // save as JPEG

              theFile.saveAs(new File (theFolderMerge + "/Merged" + bufferNumberWithZeros((a + 1), 3) ), jpgOpts)

              theFile.close(SaveOptions.DONOTSAVECHANGES)

              }

               

               

              ////////////////////////////////////

              ////// check for psd, tif or jpg //////

              function checkFor (theFile) {

              if (theFile.name.slice(-4) == ".psd" || theFile.name.slice(-4) == ".tif" || theFile.name.slice(-4) == ".jpg") {return true}

              else {return false}

              };

              ////// buffer number with zeros //////

              function bufferNumberWithZeros (number, places) {

              var theNumberString = String(number);

              for (var o = 0; o < (places - String(number).length); o++) {

              theNumberString = String("0" + theNumberString)

              };

              return theNumberString

              }

               

              When I use the first .jsx script from the 2nd comment, it saves without asking me to give file names etc... What do you think is wrong with this code?