1 Reply Latest reply on Aug 6, 2015 11:46 AM by W_J_T

    batch script quits after one file

    JinMullagin

      Hello Everyone! I'm having a bit of trouble getting this script file to batch correctly.

       

      The script will open all pdf files from the user specified folder. Then it's supposed to open the first file and check all objects for specific colours (which are die lines) and change it to a specific spot colour. Now the script works perfectly if I run the script on one file. Now that I've tried to batch the code to execute on each file, it seems to fail after the first one.

       

      I can't think of what would be causing this to happen. Please help!

       

      Below is the code:

       

      // Main Code [Execution of script begins here]
      app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
      var sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
      
      
      // Select the source folder.
      sourceFolder = Folder.selectDialog( 'Select the folder with your PDFs you wish to batch');
      // If a valid folder is selected
      if ( sourceFolder != null )
      {
          files = new Array();
          fileType = "*.pdf"; //prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
          // Get all files matching the pattern
          files = sourceFolder.getFiles( fileType );
          if ( files.length > 0 )
          {
              destFolder = sourceFolder;
              alert(files.length);
              for ( i = 0; i < files.length; i++)
              {
                 sourceDoc = app.open(files[i]); // returns the document object
                 var docRef = sourceDoc;
                 //$.writeln(sourceDoc);
                 //$.writeln(i);
                 with (docRef) {
                   dieCutFind(docRef);
                   //var pdfSaveOptions = new PDFSaveOptions();
                   //sourceDoc.saveAs (destFolder ,  pdfSaveOptions);
                   //sourceDoc.close();
               }
       } 
          }
          else
          {
              alert( 'No matching files found' );
          }
      
      }
      //////////////////////////////////////////////////////////////////////////
      function dieCutFind(docRef){
          //'use strict';
          var atomicBlackExists = "no";
          var atomicRedExists = "no";
          var redExists = "no";
          var blueExists = "no";
          var pinkExists = "no";
          var orangeExists = "no";
          //var doc = docRef;
          var  doc = app.activeDocument;
      
      
          if ( doc.pathItems.length > 0 ) {
          thePaths = doc.pathItems;
          numPaths = thePaths.length; 
      
      
              for ( i = 0; i < doc.pathItems.length; i++ ) {
                  pathArt = doc.pathItems[i];
                  var fillColor = pathArt.fillColor
                  var red = pathArt.strokeColor.red;
                  var green = pathArt.strokeColor.green;
                  var blue = pathArt.strokeColor.blue;
                  if(red !==undefined || green !==undefined || blue !==undefined){
                      if (fillColor !== undefined){
                          if ( pathArt.parent.typename != "CompoundPathItem" && red== 201 && green == 11 && blue == 6) {
                              //Atomic Red
                              AtomicRed();
                              pathArt.selected = true;
                              pathArt.strokeColor = app.activeDocument.swatches.getByName("Atomic Red GCS-293").color;
                              pathArt.selected = false;
                          }
                      }
                  }
              }
          } 
      ///////////////////////////////////////////////////////////////////////////
          function AtomicRed() {
              if (atomicRedExists !== "yes"){
                  atomicRedExists = "yes";
                  var doc = app.activeDocument;
                  // Create the new spot
                  var newSpot = doc.spots.add();
                  // Define the new color value
                  var newColor = new CMYKColor();
                  newColor.cyan = 14.29;
                  newColor.magenta = 100;
                  newColor.yellow = 100;
                  newColor.black = 5.01;
                  // of the new Spot's color. The spot color can then
                  // be applied to an art item like any other color.
                  newSpot.name = "Atomic Red GCS-293";
                  newSpot.colorType = ColorModel.SPOT;
                  newSpot.color = newColor;
                  var newSpotColor = new SpotColor();
                  newSpotColor.spot = newSpot;
              }
          };
      };
      
      
      
      
      
      
      
        • 1. Re: batch script quits after one file
          W_J_T Level 4

          At a quick glance:

           

          1. ) Why the with code inside the file loop ?

          with (docRef) { } //  ?

           

          2.) In your functions you are passing in "docRef" as a parameter, but then using "doc" throughout both dieCutFind() & AtomicRed(), that needs addressed / resolved in both. You are also using docRef to pass into dieCutFind() in the files loop, you can just pass in sourceDoc directly to reduce the confusion.

           

          3.) You should wrap everything in a function once other things are resolved.

           

          Sorry don't have time to look closer, hope something helps.