4 Replies Latest reply on May 22, 2013 1:06 PM by doekewartena

    [basiljs] remove all pages except one

    doekewartena Level 1

      In a script i create pages, export to pdf, remove pages and then create again etc.

      At the moment i use this:

       

      [code]

      for(var j = b.pageCount(); j > 1; j--) {

                b.removePage(j);

      }

      [/code]

       

       

       

      But it is really slow. I think indesign does a lot of calculating after removing a page (due link textfields) so it would be more efficient to remove all the pages at once.

      Now my question is how can i do that?

       

      I tested and if i delete about 5500 empty pages with the script it takes around 8 minutes.

      If i select them all in indesign and then delete it takes around 10 seconds.

       

      Another way to speed up a litle is to clear all the content. It will still take long but probably a bit faster.

      Thing is i have

       

      b.clear(b.doc());

       

      but this remove the master page as well and i need to keep the master page.

       

      In short, how can i remove all pages (except one since indesign needs one page) at once?

        • 1. Re: [basiljs] remove all pages except one
          milligramme Level 3

          Hi,

           

          i do removing many pages in indesign with

           

          • use documentPreferences.pagesPerDocument (faster)
          • use while or for loop
          • close doc without saving, and open again. (in some case)

           

          here is test code, run with old Macbook Air + CS5 + OSX10.7

           

           

          var pp = function (args) {
            $.writeln(args);
          }
          
          var remove_all_pages_use_pref = function (doc) {
            pp("start deleting (use preferences)");
            $.hiresTimer;
            doc.documentPreferences.pagesPerDocument = 1;
            pp("end deleting");
            pp($.hiresTimer);
          }
          
          var remove_all_pages_use_loop = function (doc) {
            var i = doc.pages.length;
            pp("start deleting (while --)");
            $.hiresTimer;
          
            while (i--) {
              if (i==1) {break;}
              doc.pages[i].remove();
            }
            pp("end deleting");
            pp($.hiresTimer);
          }
          
          function main() {
            var doc = app.documents.add();
            $.hiresTimer;
            doc.documentPreferences.pagesPerDocument = 5500;
            pp('# create 5500 pages');
            pp($.hiresTimer);
            var indd = new File("~/Desktop/5500.indd");
            doc.save(indd);
          
            pp("------------");
            remove_all_pages_use_pref(doc);
            doc.close(SaveOptions.NO);
          
            pp("------------");
            pp("# try another way");
            var doc2 = app.open(indd);
            remove_all_pages_use_loop(doc2);
            doc2.close(SaveOptions.NO);
          }
          main();
          
          

           

          result

          # create 5500 pages
          30395026
          ------------
          start deleting (use preferences)
          end deleting
          28215999
          ------------
          # try another way
          start deleting (while --)
          end deleting
          268641063
          

          thanks

          mg

          • 2. Re: [basiljs] remove all pages except one
            doekewartena Level 1

            looks promosing!

             

            How can i do this on the current document?

            I tried:

                var doc = app.documents.current();

             

            And i did found a html reference for indesign but if i did a search on app i had more then 500 hits and the same for document so that wasn't really helping.

             

            thanks

            • 3. Re: [basiljs] remove all pages except one
              Larry G. Schneider Adobe Community Professional & MVP

              var doc = app.activeDocument should do it.

              • 4. Re: [basiljs] remove all pages except one
                doekewartena Level 1

                thanks!

                 

                so in short:

                 

                var doc = app.activeDocument;

                doc.documentPreferences.pagesPerDocument = 1;