7 Replies Latest reply on Oct 30, 2008 8:29 AM by (Rasmus_Olsen)

    Export each page in a pagerange

      Dear all,

      How do I export each page in a pagerange using js - eg. I enter a pagerange "2-4,5" and InDesign exports 4 files with one page pr. file?

      From the examples included with InDesign I know how to export all the pages in a document as 1 page pr. export, or export a pagerange to one file (4 pages in one PDF file). But I can't seem to figure out how to couple a defined pagerange with looping through the document pages..

      Kind regards,
      Rasmus Olsen
        • 1. Re: Export each page in a pagerange
          Level 1
          This is what I've got so far. Its the looping going on down in the else statement I need a little assistance to. My idea was to stop looping when myPageArray are found. Then set the myPageName. But this won't work. It loops through all the pages and sets myPageName to the last page of the document.

          var myDocument = app.activeDocument;
          var myDocumentName = myDocument.name;
          var myFolder = Folder.selectDialog ("Choose a Folder");
          var myPageRange = "2-3";
          var myPageArray = myPageRange.split(/-|,/);
          myPageName;

          if (myPageRange == undefined) {
          //snip
          }
          else {
          for (var myCounter = 0; myCounter < myDocument.pages.length; myCounter++) {
          if (myDocument.pages.length[myCounter] == myPageArray) {
          myPageName = myDocument.pages.item(myCounter).name;
          }
          }
          with (app.pdfExportPreferences) {
          pageRange = myPageRange;
          viewPDF = false;
          }
          myFilePath = myFolder + "/" + myDocumentName + "_" + myPageName + ".pdf";
          myFile = new File(myFilePath);
          myDocument.exportFile(ExportFormat.pdfType, myFile, false);
          }

          Sincerly,
          Rasmus
          • 2. Re: Export each page in a pagerange
            Level 1
            Spaces and linebreaks got ignored *sigh*
            • 3. Re: Export each page in a pagerange
              [Jongware] Most Valuable Participant
              The 'split' command does just that -- it splits the input string into severat output string in the array, using the character you give to separate the strings. So, with an input of "2-4,5" your

              >split(/-|,/);

              returns an array of length 3, with elements '2', '4', and '5'.

              I think you have to parse the input manually. Something like
              * get the first number
              * if a '-' follows, get the next number, and loop over these
              * if a ',' follows, output the one you got and restart the loop
              * if no more input, you're done
              • 4. Re: Export each page in a pagerange
                Peter Kahrel Adobe Community Professional & MVP
                Rasmus,

                The thread: http://www.adobeforums.com/webx/.59b63a04/2 dealt with printing individual pages, but should work for exporting as well.

                Peter
                • 5. Re: Export each page in a pagerange
                  [Jongware] Most Valuable Participant
                  That expand function is really neat! Stored for further scrutiny ...

                  Rasmus, using the expand function on your string it resolves "2-4,6" into "2,3,4,6", and that can be taken apart with split.
                  • 6. Re: Export each page in a pagerange
                    Level 1

                    var myDocument = app.activeDocument;
                    var myDocumentName = myDocument.name;
                    var myFolder = Folder.selectDialog ("Choose a Folder");
                    var unranged_array = unrange ("1-3,4")

                    function unrange (s)
                       {
                       return s.replace (/(\d+)-(\d+)/g, expand)
                       }

                    function expand ()
                       {
                       var expanded = [];
                       var start = Number (arguments[1]);
                       var stop = Number (arguments[2]);
                       for (var i = start; i <= stop; i++)
                          expanded.push (i);
                       return expanded
                    }

                    //alert(unranged_array);
                    mySplit = unranged_array.split(',');

                    for(var myCounter = 0; myCounter < mySplit.length; myCounter++) {
                    myPageName = myDocument.pages.item(myCounter).name;
                    //alert(myPageName)
                    with(app.pdfExportPreferences) {
                      pageRange = myPageName;
                      viewPDF = false;
                    }
                    myFilePath = myFolder + "/" + myDocumentName + "_" + myPageName + ".pdf";
                    myFile = new File(myFilePath);
                    myDocument.exportFile(ExportFormat.pdfType, myFile, false);
                    }


                    And I even learned how to use the pre tag. Wow, this makes me wet :-)

                    Thank you gentlemen, you are the best.

                    Rasmus
                    • 7. Re: Export each page in a pagerange
                      Level 1
                      Ups, I messed up var myPageName. No matter what my pagerange was I kept getting page 1-4 (I'm testing this with a 4-page doc).
                      This appears to be working:


                         for(var myCounter = 0; myCounter < mySplit.length; myCounter++) {

                         myPageName = mySplit[myCounter];

                         with(app.pdfExportPreferences) {

                         pageRange = myPageName;

                         viewPDF = false;

                         }

                         myFilePath = myFolder + "/" + myDocumentName + "_" + myPageName + ".pdf";

                         myFile = new File(myFilePath);

                         myDocument.exportFile(ExportFormat.pdfType, myFile, false);

                         }