4 Replies Latest reply on May 24, 2010 11:57 PM by Pengwill

    Can't figure out how to make this script work

    Pengwill

      Hey Folks,

       

      Anyone here who would like to look at the following script and tell me where I made the error(s)?

      I want to put this script in a Batch so that I can run it on a directory of files all with filenames like

       

      Q-CAT 2010-01 Covers_v7bP01x.pdf and

      Q-CAT 2010-01 Covers_v7bP02x.pdf

       

      I put this script in the correct spot, but nothing happens... I tried the debugger console, and I get:

       

      Begin Job Code
      Processing File #1
      State P01 detected
      Batch aborted on run #1Error: NotAllowedError: Security settings prevent access to this property or method.
      End Job Code

       

      This is the script, it's supposed to run in Acrobat 9:

       

      /* Q-CatCoverPagesSaveAs */

       

      /* this script Crops CatCovers' Spreads and saves it into separate pages */

       

      /* ©20100521 - AOP-Creatives, Willy Croezen */

       

      // Begin Job

       

      if ( typeof global.counter == "undefined" ) {

       

      console.println("Begin Job Code");

       

      global.counter = 0;

       

      // insert beginJob code here

       

      }

       

      // Main Code to process each of the selected files

       

      try {

       

      global.counter++

       

      console.println("Processing File #" + global.counter);

       

      // insert batch code here.

       

      function doCropPages()


         {
         this.setPageBoxes({
            cBox: "Crop",
            rBox: cropLeft
            });
          app.beginPriv();
      console.println("Save LeftPageName");
      //       this.saveAs(this.path.slice(1, fname.indexOf(pageSide)) + leftPageName);
          app.endPriv();

       

         this.setPageBoxes({
            cBox: "Crop",
            rBox: cropRight
            });
         app.beginPriv();
      console.println("Save RightPageName");
      //      this.saveAs(this.path.slice(1, fname.indexOf(pageSide)) + rightPageName);
         app.endPriv();
         }

       

      //Declaration List
      var Units = "mm";

       

      var i = this.path.search(/[^:/]+\.pdf$/);
      var fname = this.path.slice(i, this.path.length - 4);
      var cropRect = this.getPageBox("Crop");

       

      //Check if filename contains P01, indicating the Outside spread of a Catalog Cover. If so, crop accordingly
      if (fname.indexOf("P01") != 0)
         {
      console.println("State P01 detected");
         var pageSide = "P01";
         var leftPageName = "P260.pdf";
         var rightPageName = "P001.pdf";

       

         var cropLeft = new Array();
            cropLeft[0] = cropRect[0];
            cropLeft[1] = cropRect[1];
            cropLeft[2] = cropRect[2];
            cropLeft[3] = cropRect[3] + 277.5;

       

         var cropRight = new Array();
            cropRight[0] = cropRect[0];
            cropRight[1] = cropRect[1];
            cropRight[2] = cropRect[2] + 223.5;
            cropRight[3] = cropRect[3] - 62;
         doCropPages();
         }
      else
         {
         //If filname does NOT contain P01, check if filename contains P02, indicating the Inside spread of a Catalog Cover. If so, crop accordingly
         if (fname.indexOf("P02") != 0)
            {
      console.println("State P02 detected");
            var pageSide = "P02";
            var leftPageName = "P002.pdf";
            var rightPageName = "P259.pdf";
        
            var cropLeft = new Array();
               cropLeft[0] = cropRect[0];
               cropLeft[1] = cropRect[1];
               cropLeft[2] = cropRect[2];
               cropLeft[3] = cropRect[3] + 223.5;

       

            var cropRight = new Array();
               cropRight[0] = cropRect[0];
               cropRight[1] = cropRect[1];
               cropRight[2] = cropRect[2] + 277.5;
               cropRight[3] = cropRect[3];
            doCropPages();
            }
         else
            {
            // If filename doesn't contain P01 OR P02, give Error notice
      console.println("Document is not correctly named! Should have P01 or P02 in it");
            }
         }
      }
      catch(e) {
      console.println("Batch aborted on run #" + global.counter + "Error: " + e);
      delete global.counter; // so we can try again, and avoid End Job code
      event.rc = false; // abort batch
      }
      // End Job
      if ( global.counter == global.FileCnt ) {
      console.println("End Job Code");
      // insert endJob code here
      // may have to remove any global variables used in case user wants to run
      // another batch sequence using the same variables, for example...
      delete global.counter;
      }
        • 1. Re: Can't figure out how to make this script work
          BKD2012

          Hi,

           

           

          I'm not too familiarized with the API, but the error indicates that the script needs security privileges.

           

          1Error: NotAllowedError: Security settings prevent access to this property or method.

          Maybe you have to give to the script this privileges with app.trustedFunction.

          1 person found this helpful
          • 2. Re: Can't figure out how to make this script work
            Pengwill Level 1

            Okay made some progress... Found out that the security error was due to the

             

                app.beginPriv(); and

                app.endPriv();

             

            pieces of code around the save-routine.

            Took them out. The script now looks like below.

            It does something now... it actually crops pages and saves them to the path I indicated: d:/temp

            But... it doesn't seem to process the first document, or overwrites the results of the first loop with the results of the second loop of the batch, and it doesn't recognice the P02 file...

            I end up with two files: 001.pdf and 260.pdf which turn out to hold the CONTENTS of the original file with P02 in the filename...

            I think it might have to do with variables getting re-used or something.

             

            So...

            Any ideas?

             

            /* Q-CatCoverPagesSaveAs */
            /* this script Crops CatCovers' Spreads and saves it into separate pages */
            /* ©20100521 - AOP-Creatives, Willy Croezen */

            // Begin Job
            if ( typeof global.counter == "undefined" ) {
            console.println("Begin Job Code");
            global.counter = 0;
            // insert beginJob code here
            }
            // Main Code to process each of the selected files
            try {
            global.counter++
            console.println("Processing File #" + global.counter);
            // insert batch code here.

            function doCropPages()
               {
               this.setPageBoxes({
                  cBox: "Crop",
                  rBox: cropLeft
                  });
            console.println("Save LeftPageName to: D:/temp/" + leftPageName);
            //    app.beginPriv();
                   this.saveAs("/D/temp/" + leftPageName);
            //    app.endPriv();

               this.setPageBoxes({
                  cBox: "Crop",
                  rBox: cropRight
                  });
            console.println("Save RightPageName to: D:/temp/" + rightPageName);
            //   app.beginPriv();
                  this.saveAs("/D/temp/" + rightPageName);
            //   app.endPriv();
               }

            //Declaration List
            var Units = "mm";

            var i = this.path.search(/[^:/]+\.pdf$/);
            var fname = this.path.slice(i, this.path.length - 4);
            var cropRect = this.getPageBox("Crop");

            //Check if filename contains P01, indicating the Outside spread of a Catalog Cover. If so, crop accordingly
            if (fname.indexOf("P01") != 0)
               {
            console.println("State P01 detected");
               var pageSide = "P01";
               var leftPageName = "P260.pdf";
               var rightPageName = "P001.pdf";

               var cropLeft = new Array();
                  cropLeft[0] = cropRect[0];
                  cropLeft[1] = cropRect[1];
                  cropLeft[2] = cropRect[2] - 645;
                  cropLeft[3] = cropRect[3];

               var cropRight = new Array();
                  cropRight[0] = cropRect[0] + 634;
                  cropRight[1] = cropRect[1];
                  cropRight[2] = cropRect[2] - 176;
                  cropRight[3] = cropRect[3];
               doCropPages();
               }
            else
               {
               //If filname does NOT contain P01, check if filename contains P02, indicating the Inside spread of a Catalog Cover. If so, crop accordingly
               if (fname.indexOf("P02") != 0)
                  {
            console.println("State P02 detected");
                  var pageSide = "P02";
                  var leftPageName = "P002.pdf";
                  var rightPageName = "P259.pdf";
              
                  var cropLeft = new Array();
                     cropLeft[0] = cropRect[0];
                     cropLeft[1] = cropRect[1];
                     cropLeft[2] = cropRect[2] - 634;
                     cropLeft[3] = cropRect[3];

                  var cropRight = new Array();
                     cropRight[0] = cropRect[0] + 645;
                     cropRight[1] = cropRect[1];
                     cropRight[2] = cropRect[2];
                     cropRight[3] = cropRect[3];
                  doCropPages();
                  }
               else
                  {
                  // If filename doesn't contain P01 OR P02, give Error notice
            console.println("Document is not correctly named! Should have P01 or P02 in it");
                  }
               }
            }
            catch(e) {
            console.println("Batch aborted on run #" + global.counter + "Error: " + e);
            delete global.counter; // so we can try again, and avoid End Job code
            event.rc = false; // abort batch
            }
            // End Job
            if ( global.counter == global.FileCnt ) {
            console.println("End Job Code");
            // insert endJob code here
            // may have to remove any global variables used in case user wants to run
            // another batch sequence using the same variables, for example...
            }
            delete global.counter;

            • 3. Re: Can't figure out how to make this script work
              AcroBishop Level 1

              I'm not 100% sure of the problem, but here are the 2 things I noticed that might improve your chance of success.

               

              1. You could use if, else if, else to paint a clearer picture of your conditions testing when checking for the content of the file name (your current structure is if, else, <then> if, else)

               

              2.  Is there any chance that letter casing could be an issue? Perhaps you should test using a RegExp instead of indexOf  i.e. if(/p01/i.test(fname) {

              • 4. Re: Can't figure out how to make this script work
                Pengwill Level 1

                Hy Folks,

                 

                This thread can be closed, as I fixed the script! It turned out that I needed to put the function definition above the body-code for the script to find it. After that it was small details.

                I created a conversion variable (UnitsConv) for the margins so you  can simply enter the desired dimensions in mm, instead of px, which I  thought was easier.

                This script now perfectly processes the files  you put to it in a batch-operation (if correctly named with P01 and P02  in the filename.) and saves the resulting pages into the specified  temp-directory. This time the filenames reflect the original filenames,  for easy identification later, with the new pagenumbers appended to  them.

                For your information (and perhaps for use as the base for a custom  script for yourself) I give you the final working version below.

                 

                This script will be trimmed down and become part of a larger script, or scriptset, which I will be using to automate the workflow of creating al different versions of lowres and highres catalogs that we need.

                 

                Kind regards,

                 

                Willy Croezen

                 

                 

                /* Q-CatCoverPagesSaveAs v1.0*/
                /* this script Crops CatCovers' Spreads and saves it into separate pages */
                /* ©20100521 - AOP-Creatives, Willy Croezen */

                 

                // Begin Job
                if ( typeof global.counter == "undefined" ) {
                console.println("Begin Job Code ");
                global.counter = 0;
                // insert beginJob code here
                }
                // Main Code to process each of the selected files
                try {
                global.counter++
                console.println("Processing File #" + global.counter);
                // insert batch code here.

                 

                function doCropPages(crL,crR,lPName,rPName)
                   {
                   this.setPageBoxes({
                      cBox: "Crop",
                      rBox: crL
                      });
                      console.println("Save LeftPageName to: D:/temp/" + lPName);
                      this.saveAs("/D/temp/" + lPName);

                 

                   this.setPageBoxes({
                      cBox: "Crop",
                      rBox: crR
                      });
                      console.println("Save RightPageName to: D:/temp/" + rPName);
                      this.saveAs("/D/temp/" + rPName);
                   }

                 

                //Declaration List
                var UnitsConv = (72 / 25.4);
                var i = this.path.search(/[^:/]+\.pdf$/);
                var fname = this.path.slice(i, this.path.length - 4);
                var cropRect = this.getPageBox("Crop");

                 

                //Check if filename contains P01, indicating the Outside spread of a Catalog Cover. If so, crop accordingly
                if (fname.indexOf("P01") != -1)
                   {
                console.println("Value fname.indexOf(P01) = " + fname.indexOf("P01"));
                console.println("State P01 detected");
                   var pageSide = "P01";
                   var leftPageName = fname + "P260.pdf";
                   var rightPageName = fname + "P001.pdf";

                 

                // Create array of crop-values. Sequence is left, top, right, bottom
                   var cropLeft = new Array();
                      cropLeft[0] = cropRect[0];
                      cropLeft[1] = cropRect[1];
                      cropLeft[2] = cropRect[2] - Math.floor(277.5 * UnitsConv);
                      cropLeft[3] = cropRect[3];

                 

                   var cropRight = new Array();
                      cropRight[0] = cropRect[0] + Math.floor(223.5 * UnitsConv);
                      cropRight[1] = cropRect[1];
                      cropRight[2] = cropRect[2] - Math.floor(62 * UnitsConv);
                      cropRight[3] = cropRect[3];
                   doCropPages(cropLeft,cropRight,leftPageName,rightPageName);
                   }
                else
                   {
                   //If filname does NOT contain P01, check if filename contains P02, indicating the Inside spread of a Catalog Cover. If so, crop accordingly
                   if (fname.indexOf("P02") != -1)
                      {
                console.println("State P02 detected");
                      var pageSide = "P02";
                      var leftPageName = fname + "P002.pdf";
                      var rightPageName = fname + "P259.pdf";

                 

                // Create array of crop-values. Sequence is left, top, right, bottom  
                      var cropLeft = new Array();
                         cropLeft[0] = cropRect[0];
                         cropLeft[1] = cropRect[1];
                         cropLeft[2] = cropRect[2] - Math.floor(223.5 * UnitsConv);
                         cropLeft[3] = cropRect[3];

                 

                      var cropRight = new Array();
                         cropRight[0] = cropRect[0] + Math.floor(277.5 * UnitsConv);
                         cropRight[1] = cropRect[1];
                         cropRight[2] = cropRect[2];
                         cropRight[3] = cropRect[3];
                      doCropPages(cropLeft,cropRight,leftPageName,rightPageName);
                      }
                   else
                      {
                      // If filename doesn't contain P01 OR P02, give Error notice
                console.println("Document is not correctly named! Should have P01 or P02 in it");
                      }
                   }
                }
                catch(e) {
                console.println("Batch aborted on run #" + global.counter + "Error: " + e);
                delete global.counter; // so we can try again, and avoid End Job code
                event.rc = false; // abort batch
                }
                // End Job
                if ( global.counter == global.FileCnt ) {
                console.println("End Job Code");
                // insert endJob code here
                // may have to remove any global variables used in case user wants to run
                // another batch sequence using the same variables, for example...
                }
                delete global.counter;