3 Replies Latest reply on Oct 6, 2016 11:48 PM by gert verrept

    delete pages

    jakec88782761 Level 1

      Hi everyone,

       

      Im trying to delete the first and last page of a doc before exporting.

       

      This loop deletes every page and I can't figure out why..

       

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

          if (pages[i]===page[0]||pages[i]===page[-1]){

              pages[i].remove();

           }

        }

       

      The script always runs the code in the if statement no matter what page it's on.

       

      Any help?

        • 1. Re: delete pages
          geert@2imagine Level 1

          Hi,

           

          Try this:

          pages[pages.length-1].remove();
          pages[0].remove();
          • 2. Re: delete pages
            Loic.Aigon Adobe Community Professional

            Maybe that's because when you remove pages[i] when i stands for 0, the previous pages[1] reference now point to the current pages[0] object. So loop after loop pages[i] is still equaling pages[0] then leading to the removal of all the pages (except final page 1).

             

            Prefer @geert@2imagine method.

             

            Loic

            • 3. Re: delete pages
              gert verrept Level 2

              I use this one (made by MVH)

              It gives you a pagerange or a just one page you can delete, if you insert the code to delete the first and last page in the dialogbox, you have it all in one script.

               

              if (app.documents.length == 0){

                alert("There is no document open to delete pages.");

                exit();

              }

               

               

              var remove_pages = menu();

               

               

              if (remove_pages.show()) {

              number_pages = pages.editContents;

                try {

                remove_override();

                pages_remove();

                } catch (e) {}

              }

               

               

               

               

               

               

              //functions

              //--------------------------------------------------------------------

              function menu(){

              var dialoog_venster = app.dialogs.add({name:"Delete pages", canCancel:true} );

                with (dialoog_venster){

                with (dialogColumns.add() ){

                with (dialogRows.add() ){

                with (dialogRows.add() ){

                staticTexts.add({staticLabel:"Pagerange you want to delete:          "} );

                }

                }

               

                with (dialogRows.add() ){

                 

                with (dialogColumns.add() ){

                with (dialogRows.add() ){

                pages = textEditboxes.add({editContents:"", minWidth:225} ); }

                }

                }

               

                }

               

                }

                return dialoog_venster;

              }

               

               

              function pages_remove() {

               

                range_test = number_pages.indexOf("-");

               

               

                if (range_test != -1) {

                split_range = number_pages.split("-");

                start = split_range[0];

                end = split_range[1];

               

               

                number = parseInt(end) - parseInt(start) + 1;

               

                for (i=0; i<number; i++) {

                page = parseInt(end) - i;

                page = page + "";

                app.activeDocument.pages.item(page).remove();

                }

               

               

               

               

                } else {

               

                app.activeDocument.pages.item(number_pages).remove();

               

                }

               

               

              }

               

               

               

               

              function remove_override() {

               

                total_number_pages = app.activeDocument.pages.length;

               

                for (i=1; i<total_number_pages; i++) {

               

                page_items = app.activeDocument.pages.item(i).pageItems;

               

               

                for (j=page_items.length-1; j>-1; j--) {

               

                if (page_items[j].label == "") {

                if (page_items[j].overridden == true) {

                page_items[j].removeOverride();

                }

                }

                }

               

                }

               

              }