2 Replies Latest reply on Oct 24, 2016 9:37 PM by tpk1982

    Add pages until multiple of 4

    jakec88782761 Level 1

      Hello everyone,

       

      I'm trying to add the code to my script which first deletes all empty pages and then adds empty pages until the total number of pages is a multiple of 4.

       

       

      function pageInsert () {
         
      var pages = app.activeDocument.pages.everyItem().getElements();

              for ( i = 0; i<pages.length; i++ ) {
         
                      if (pages[i].pageItems.length==0) {
             
                          pages[i].remove();
             
                      }
          } //delete empty pages

      var newPages = app.activeDocument.pages.everyItem().getElements();
      if ( pages.length % 4 != 0) {  //  Add a page if the pages aren't divisible by 4.
         
         
          app.activeDocument.pages.add( LocationOptions.BEFORE, app.activeDocument.spreads[-1] );
         
         
         
          } //insert an empty page to make the document pages a multiple of 4 (for booklets)

      }

       

      This is the script and it delete the empty pages fine, it just doesn't execute the last bit of code which adds a page if it's not divisible by 4.

       

      Any ideas?

        • 1. Re: Add pages until multiple of 4
          S Hopkins Adobe Community Professional

          Hi,

          A couple of suggestions: Once you get a reference to an object, place it into a variable and use the variable reference. (e.g.: app.activeDocument). You would probably have that reference in the main section of your script, so pass it to the function.

          Here is how I might write the function:

           

          var docRef = app.activeDocument;

          pageInsert(docRef);

           

          function pageInsert (docRef) {

          var docRef = app.activeDocument;

          var pages = docRef.pages.everyItem().getElements();

                  for ( i = 0; i<pages.length; i++ ) {

                          if (pages[i].pageItems.length==0) {

                              pages[i].remove();

                          }

              } //delete empty pages

          var newPages = docRef.pages.everyItem().getElements();

              while (docRef.pages.count() % 4 != 0)  {

              docRef.pages.add( LocationOptions.BEFORE, docRef.spreads[-1] );

              } //insert an empty page to make the document pages a multiple of 4 (for booklets)

          }

          Hope this helps.

          S.Hopkins

          • 2. Re: Add pages until multiple of 4
            tpk1982 Level 4

            This is script from my end... for adding pages

             

            var myDoc=app.activeDocument
            var mylength=(myDoc.pages.length);
            var err=0
            var mydiv=(mylength%4)
            var myadd=(4-mydiv)
            if(mydiv==0){
            alert("No issue with Page count. Press OK to go next step.")}
            if(myadd!=4){
            var myconfirmation=confirm("Need to add "+myadd+" pages to make pagecount. Shall it add? ")
            if(myconfirmation){
            if(mydiv !=0){
                for (i=0;i<myadd;i++){
                var f=myDoc.pages.add(LocationOptions.AT_END)
                f.appliedMaster=null
                }
              }
            }
            }
            

             

             

            HTH

            1 person found this helpful