4 Replies Latest reply on Aug 23, 2017 7:02 AM by calvins61590516

    Remove / Delete pages with javascript (or vbs)


      There is a MyPage.Add()... There is no MyPage.remove()?

      Can I remove a given page number with a script?


      The problem:

      I have a facing pages problem. I essentially need to delete two pages, leave two pages, delete two and so on for about 2000 pages. Sure I could click and delete, but what would be the fun in that?


      The pages are created from a facing-pages datamerge. Why CS4 can't handle facing pages datamerge is beyond me... Either way, I need a work around.


      Visually in the PAGES tab, what I have is this


      Data Merge Entry Data Merge Entry

      Facing Page AFacing Page B
      Data Merge Entry AData Merge Entry A
      Data Merge Entry BData Merge Entry B
      Data Merge Entry CData Merge Entry C
      Data Merge Entry DData Merge Entry D


      Resulting in A,A,B,B,C,C,D,D, etc.



      What I need to end with is




      Resulting in A,B,C,D, etc.


      The rules: The database is untouchable. Facing Pages Side A and Side B are formated differently.


      Any and all help or information leading to a working work-around willl be handsomly rewarded the moment I get my check from a rich African uncle who, I sadly found out through an email today, had recently passed away.


      Many thanks!


        • 1. Re: Remove / Delete pages with javascript (or vbs)
          Peter Kahrel Adobe Community Professional & MVP

          >There is no MyPage.remove()?


          There is! When you add a page, you add it to the page collection, so your starting point is the page collection:


          myDoc.pages.add (...);


          To delete a page, your starting point is not the page collection, but the page itself. For instance, to delete the sixth page, you would use this:


          myDoc.pages[5].remove ();


          (JavaScript starts counting at zero, so page 6 is the fifth page.) Note the difference between pages and page[5]: the former is the collection of pages, the latter is one page.



          1 person found this helpful
          • 2. Re: Remove / Delete pages with javascript (or vbs)
            [Jongware] Most Valuable Participant

            But there is a myPage.remove()


            Are there threaded text frames on the pages you want to remove? In a script, this behaves just like in the UI: threaded text is shifted to the next frame in the thread (usually, on the next page). So if that's the case, this script will work but not the way you intended it.


            Anyway, you want to remove one page, then skip two pages -- remove two pages -- skip 2 -- etc., until the end of the document.


            This Javascript should do it:


            var curpage = 0;
            while (curpage < app.activeDocument.pages.length)
             curpage += 2;

            It boings out with an error at the end if your document contains an odd number of pages (?) because it might try to remove a page after the last one, but that's not a critical error, methinks.

            I've tested it on a 30+ document, and it correctly deleted #1, then #4 and #5, then #8 and #9, etc. Still, paranoia is a healthy lifestyle (when applied to computers), so run it on a copy, will ya.

            • 3. Re: Remove / Delete pages with javascript (or vbs)
              FigmentFilm Level 1

              Thank you both very much for the help! That code worked like a charm!



              • 4. Re: Remove / Delete pages with javascript (or vbs)

                Hi Peter,


                I saw your response here and was hoping you could assist me, I am trying to use your code to delete a page in my document (page 3 to be specific or javascript page 2). I have used the method you noted above ( myDoc.pages[5].remove (); ) however it does not seem to work. I have entered the javascript as a mouse down action on a radio button. I would really appreciate your assistance here?