4 Replies Latest reply on May 11, 2011 1:21 AM by beh_gras_beh

    Batch naming PDF files from textframe

    beh_gras_beh Level 1

      Dear people,


      Couple of days ago I asked a question about how can i adjust a script to choose a page that i want to export to PDF.

      John pointed me to the right direction but still i'm having no luck so far.


      I'm specificly having trouble with this part:


      1. This is where the user can type his number, to chose witch page has to be exported:

      var pagelist=prompt("Which pages?\nformat: 1,2,3,4");


      2. Than I have to tell the script witch pages he only has to export. (For example only page 3 and 8)

      var pages = pagelist.split(",");


      3. Than the script only has to search on the chosen pages to a textframe named "label"

      ( I manualy named a textframe on each page with the scriptlabel palet "label)


      4. The script reads the text in the specific texframe and than the script export that page to a PDF

      (so you get something like: "This is page 3.PDF" and This is page 8.PDF")


      I'm definitely doning this totally wrong but that's because i'm very new to scripting.




      This is what I have so far, the bold section I'm doing things wrong but what exactly?




      var myDoc = app.documents[0];

      app.scriptPreferences.version = "6.0"


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

      var myFolder = Folder.selectDialog ("Choose a Folder");

      if(myFolder != null){





      alert("Please open a document and try again.");


      function myExportPages(myFolder){

      var myDocument = app.activeDocument;

      var myDocumentName = myDocument.name;

      var myPDFPresets = app.pdfExportPresets;

      var myPDFPresetNames = new Array;

      for (i = 0; i < myPDFPresets.length; i++)



      var myDialog = app.dialogs.add();





      staticTexts.add({staticLabel:"PDF preset: ", minWidth:130});



      var myPDFPresetsDropdown = dropdowns.add({stringList:myPDFPresetNames,  selectedIndex:0});





      var myResult = myDialog.show({name:"ExportPages"});

      if(myResult == true){

      var myPDFPresetName = myPDFPresetNames[myPDFPresetsDropdown.selectedIndex];



      //Remove the dialog box from memory.






      doc = app.activeDocument;

      doc.sections.everyItem().includeSectionPrefix = false;




      var pagelist=prompt("Which pages?\nformat: 1,2,3,4");

         for (var p = 0; p < pagelist.length; p++)

      var pages = pagelist.split(",");

      var myArray = ['1','2','3','4','4','5','6'];

         if (pagelist <= myArray.length-1) {

            return myArray[pagelist];


         else {

            return "" + pagelist;


      var pagesObject = {};

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

        pagesObject[pagelist[i]] = 1;


      p = app.activeDocument.pages.everyItem().getElements();

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

        if (!(p[i].name in pagesObject)) P



        var myRegExp = new RegExp(":","gi");

      f = File (myFolder + "/" + p[i].textFrames.item ('label').contents + ".pdf");

      app.pdfExportPreferences.pageRange =  p[i].name;

      myDocument.exportFile(ExportFormat.pdfType,   f, false, myPDFPresets.item(myPDFPresetName));






      Thanks on those who are helping me out!

      Greetings from Holland!

        • 1. Re: Batch naming PDF files from textframe

          Do you need the complete Page which was prompted as input has to be exported as PDF or else the specifically labeled text frame from those prompted pages alone has to be exported as PDF.

          • 2. Re: Batch naming PDF files from textframe
            beh_gras_beh Level 1

            I'ts that last you said.


            On each page there is a textframe and labeled in the script label with "label"


            When I run the script every page comes out like this:


            This is page one.pdf

            Than comes page two.pdf

            And after two comes three.pdf

            And last you have page four.pdf


            Now with this script I only want to export "This is page one.pdf" and "And last you have page four.pdf".

            I'm sorry for my poor English as dutch is my native language.


            Please let me know if you understand it a little bit.


            Thanks for the reply though!

            • 3. Re: Batch naming PDF files from textframe
              Haakenlid Level 3

              I don't really understand what the bold part of your code is supposed to do.


              I would just remove it and replace the last part of the script with something like this:


              I use a regular expression to check if the pagenumber is part of the pagelist string, and then export the pages that are.



              var pagelist=prompt("Which pages?\nformat: 1,2,3,4");
              for (i = 0; i < myDoc.pages.length; i++) {
                  if (pagelist.match("\\b"+myDocument.pages[i].name+"\\b")){
                      myFile = File (myFolder + "/" +myDocument.pages[i].textFrames.item ('label').contents + ".pdf");
                      app.pdfExportPreferences.pageRange = myDocument.pages[i].name;
                      myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFPresets.item(myPDFPresetName));
              • 4. Re: Batch naming PDF files from textframe
                beh_gras_beh Level 1

                That's it!


                I was totally going the wrong direction.

                Thank you very, very much for the help!



                P.s. If somebody want's the total script with a .Indd example I will post it, I think it's a very handy script for everybody.