5 Replies Latest reply on Apr 22, 2016 5:00 AM by natrev

    Delete blank pages only at the end of the document

    aviR

      Hii all

      I just wanted to delete the blank pages that are present at the end of the document. If i search for page contents then it is deleting pages from the middle of the document as well. How can I limit it?

      Please suggest

      Thanks

        • 1. Re: Delete blank pages only at the end of the document
          tpk1982 Level 4

          try this,

           

          var pag=app.documents[0].pages;
          for(var i=pag.length; i>=0; i--){ 
              if(pag[i-1].pageItems.length==0){ 
                  pag[i-1].remove(); 
                  } 
              else{break;}
              } 
          
          2 people found this helpful
          • 3. Re: Delete blank pages only at the end of the document
            Harbs. Level 6

            Here's a version of that script with an option to only remove pages at the end:

             

            //DESCRIPTION Removes all empty pages in the current document
            // (c) Harbs www.in-tools.com
            var appVersion = parseFloat(app.version);
            if(appVersion<6){main();}
            else{app.doScript (main,undefined,undefined,UndoModes.FAST_ENTIRE_SCRIPT,"Remove Empty Pages")}
            
            function main(){
                var d=app.dialogs.add({name:"Define Empty Pages"});
                var col = d.dialogColumns.add();
                var radioBtns = col.radiobuttonGroups.add();
                radioBtns.radiobuttonControls.add({staticLabel:"No Objects",checkedState:true});
                radioBtns.radiobuttonControls.add({staticLabel:"No Text"});
                col.staticTexts.add();
                var checkbox = col.checkboxControls.add({staticLabel:"At end of Doc",checkedState:false});
                if(!d.show()){d.destroy();return}
                var emptyOption = radioBtns.selectedButton;
                var onlyAtEnd = checkbox.checkedState;
                d.destroy();
                var pages = app.documents[0].pages.everyItem().getElements();
                for(var i = pages.length-1;i>=0;i--){
                    var removePage = true;
                    if(pages[i].pageItems.length>0){
                         var items = pages[i].pageItems.everyItem().getElements();
                         if(emptyOption==0){
                             if(items.length>0){removePage=false}
                         } else {
                            for(var j=0;j<items.length;j++){
                                if(!(items[j] instanceof TextFrame)){removePage=false;break}
                                if(items[j].contents!=""){removePage=false;break}
                            }
                        }
                    }
                    if(i==0 && app.documents[0].pages.length==1){removePage = false}
                    if(removePage){pages[i].remove()}
                    else if(onlyAtEnd){break;}
                }
                //beep();
                alert("Done!");
            }
            
            • 4. Re: Delete blank pages only at the end of the document
              tpk1982 Level 4

              Thanks Harbs for your script..

               

              I hope aviR may use this with dialog if he needed for specific (text frame or other objects) ...

              • 5. Re: Delete blank pages only at the end of the document
                natrev Level 3

                Hi tpk1982,

                 

                Your script is simply working like charm.. and much Appreciated!