11 Replies Latest reply on Feb 24, 2010 2:32 PM by Danny Noonan

    Naming PDF at export using a portion of IDCS4 File name(JS)

    Danny Noonan Level 1

      I have a script that will use a PDF Export Preset to export each page of a document individually. The script uses the ID document's name and adds an "_Final".

      For example:

      ID docs name:

      138-140 TEST 10_WORKER.indd

       

      Final PDF's name:

      138-139 TEST 10_WORKER_Final_1.pdf

      138-140 TEST 10_WORKER_Final_2.pdf

      138-140 TEST 10_WORKER_Final_3.pdf

       

      The first three numbers represent the first page number in the document. I would like it to use these numbers and progressively add one to the following PDF page numbers. I am looking for it to do this while using our standard naming convention which looks like this. . .

       

      MYTEST_138.pdf

      MYTEST_139.pdf

      MYTEST_140.pdf

       

      Any guidance would be greatly appreciated. Thanks!

        • 1. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
          Kasyan Servetsky Level 5

          if(app.documents.length != 0){
          var myFolder = Folder ("~/Desktop");
          if(myFolder != null){
          var myPageName, myFilePath, myFile;
          var myDocument = app.activeDocument;
          var myDocumentName = myDocument.name;
          var myBaseName = myDocumentName + "_" ;
          for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
              if (myDocument.pages.item(myCounter).appliedSection.name != ""){
                  myDocument.pages.item(myCounter).appliedSection.name = "";
              }
          myPageName = myDocument.pages.item(myCounter).name;
          app.pdfExportPreferences.pageRange = myPageName;

           

          var cornumber = pagenum(myPageName)
          myFilePath = myFolder + "/" + myBaseName + cornumber + ".pdf";
          myFile = new File(myFilePath);
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
          myDocument.exportFile(ExportFormat.pdfType, myFile, false, "Type the name of your PDF preset here");
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
          }
              }
                  }
              else{
                  alert("No documents are open. Please open a document and try again.");
                  exit();
              }
             
          alert("Done");

           

          function pagenum(myNumber){
              if (myNumber >= 1 && myNumber <= 9)
              {
              x =  "0" + "0" + myNumber;
              }
              else if (myNumber >= 10 && myNumber <= 99)
              {
              x = "0" + myNumber;
              }
              else if (myNumber >= 100 && myNumber <= 999)
              {
              x = myNumber;
              }
          return x
          }

          1 person found this helpful
          • 2. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
            Danny Noonan Level 1

            Thanks for your reply Kasyan.

            That script does produce individual PDFs perfectly. However, the naming isn't exactly what I was hoping for. To clarify what I'm trying to do, in the IDCS4 name, 138-140 TEST 10_WORKER.indd, I want to keep the only the first three characters, in this case they are 138. This will be the page number used for the first PDF. The second PDF produced would use 139(138+1), and the third would use 140 . . . and so on.

            These are the names I got after running the posted script:

             

            138-140 TEST 10_WORKER_001.pdf

            138-140 TEST 10_WORKER_002.pdf

            138-140 TEST 10_WORKER_003.pdf

             

            Generally, all I want to do is replace all the stuff after "138" with ".pdf" and add "MYTEST_" before 138, so the finals would MYTEST_138.pdf, MYTEST_139.pdf,MYTEST_140.pdf.

             

            Thanks again for your help Kasyan. Anymore would be greatly appreciated.

            • 3. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
              Kasyan Servetsky Level 5

              Here is a new version, Danny:

               

              if(app.documents.length != 0){
              var myFolder = Folder ("~/Desktop");
              if(myFolder != null){
              var myPageName, myFilePath, myFile;
              var myDocument = app.activeDocument;
              for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
                  if (myDocument.pages.item(myCounter).appliedSection.name != ""){
                      myDocument.pages.item(myCounter).appliedSection.name = "";
                  }
              myPageName = myDocument.pages.item(myCounter).name;
              app.pdfExportPreferences.pageRange = myPageName;
              myFilePath = myFolder + "/" + "MYTEST_" + pad000(myPageName) + ".pdf";
              myFile = new File(myFilePath);
              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
              myDocument.exportFile(ExportFormat.pdfType, myFile, false, "Type the name of your PDF preset here");
              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
              }
                  }
                      }
                  else{
                      alert("No documents are open. Please open a document and try again.");
                      exit();
                  }
                 
              alert("Done");

               

              function pad000(myNumber) {
                  return ('000'+myNumber).match (/...$/)[0];
              }

               

              The script currently names pdf files accordingly to the page names in Pages panel, adding zeroes to the left, if necessary. Is that what you need?

               

              Kasyan

              1 person found this helpful
              • 4. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                Danny Noonan Level 1

                Kasyan,

                Thanks for your help. Sorry I haven't been able to post, my boss was cracking the whip. But this last one is very close. The first part is perfect. The numbering is the only hurdle.

                Unfortunately, the page panels always start at '1' for this particular project. They do not reflect the actual page number, so that method will not work.

                The only reference to the page number is in the name of the .indd. It's always the first 3 characters in the name.

                example:     138-140 TEST 10_WORKER.indd

                138 is the page number we need used in the final.

                Is there a way to right the script so it will keep only the first three letters of the .indd name?

                 

                Thanks again Kaysan.

                 

                Your assistance is appreciated.

                 

                Danny

                • 5. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                  Danny Noonan Level 1

                  Hello Scripters,

                  I thought this was impossible, but I was going through the Adobe PhotoShop Scripting Forum yesterday and ran across this thread concerning RegExp. . .

                   

                  http://forums.adobe.com/message/1905324#1905324

                   

                  RegExp is something I am not familiar with-at all. The syntax looks pretty confusing. That's why I am posting here again. I was hoping someone is familiar with RegExp and could help my complete the excellent scripts Kasyan has generously provided.

                  This looks like what is needed to use only a portion of an .indd file name in the PDF export script. I would like to use only the first three digits in the file name, and then proceed to add one to that number to any PDF that follows.

                  Simply put, if the file name is 138-140 TEST 10_WORKER.indd, the first PDF produced would be MYTEST_138.pdf, for example. The second MYTEST_139.pdf, and third, MYTEST_140.pdf.

                   

                  Thanks for any help you can provide in advance.

                   

                  Danny

                   

                  One other thing, the example I gave above has three pages in the document. I just want to clarify, we can get these documents ranging from one page to twenty five pages.

                   

                  Thanks Again.

                  • 6. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                    Shonkyin Level 3

                    Hi Danny,

                     

                    It will help you sure.

                     

                     

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

                    var myFolder = Folder ("~/Desktop");

                    if(myFolder != null){

                    var myPageName, myFilePath, myFile;

                    var myDocument = app.activeDocument;

                    var my_suffix = parseInt((app.activeDocument.name).substr(0,3))

                    if (typeof (my_suffix) != 'number')

                    {

                    exit();

                    }

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

                        if (myDocument.pages.item(myCounter).appliedSection.name != ""){

                            myDocument.pages.item(myCounter).appliedSection.name = "";

                        }

                    myPageName = myDocument.pages.item(myCounter).name;

                    app.pdfExportPreferences.pageRange = myPageName;

                    myFilePath = myFolder + "/" + "MYTEST_" + (my_suffix + myCounter) + ".pdf";

                    myFile = new File(myFilePath);

                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;

                    myDocument.exportFile(ExportFormat.pdfType, myFile, false, "Type the name of your PDF preset here");

                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                    }

                        }

                            }

                        else{

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

                            exit();

                        }

                       

                    alert("Done");

                     

                     

                     

                    Shonky

                    • 7. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                      Danny Noonan Level 1

                      Wow, that works perfect!  Thanks for your help Shonky.

                       

                      var my_suffix = parseInt((app.activeDocument.name).substr(0,3))

                      if (typeof (my_suffix) != 'number')

                       

                      This is syntax I've never seen before. Can all of this be found in the OMV?

                      What does the (0,3) mean? How does this work?

                       

                      If you have time to further explain, I'm listening. Just when I think I am starting to understand JavaScript, I see something like this.

                       

                      Thanks again for your help Shonky, I appreciate it.

                       

                      Danny

                      • 8. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                        [Jongware] Most Valuable Participant

                        The substr(start,length) stuff is in the OMV (under Javascript's Core Classes, class String -- and if you are interested in further developing your JS skills you might want to check my version of this clumsy system!).

                         

                        Shonky cuts off the first three characters and instructs Javascript to forcibly convert them to a number -- the parseInt command (and that's in the OMV Core Classes under "Global"). parseInt returns the numerical value of the string if it contains digits, or the special value NaN ("Not-a-Number") if it's not. Hence the 'typeof' check -- which, incidentally, I didn't know, but I can guess at what it does. I bet it returns 'true' if the variable contains a number, 'false' if not; and NaN is per definition not a number. (That's computer logic, that is!)

                         

                        The check is necessary because you want to add something to it as a number; and JS also allows 'adding to a string'. Suppose the first three characters are "hey", adding 1 to it would return the syntactically correct result "hey+1", but it's not what you want here (more computer logic!).

                         

                        --

                         

                        The GREP you point to is quite a bit smarter than assuming the first three, and only three, characters are in fact digits. Some testing shows that this

                         

                        n = decodeURI(app.activeDocument.name).replace(/^(\d+).*/, '$1');

                        alert (n);

                         

                        returns one single string consisting of all digits at the front of your file name. I had to try this out one character at a time ... but, eventually, it boiled down to this:

                         

                        ^(\d+).*

                         

                        ^  start of text

                        (..) group number 1

                        \d any digit

                        + one or more of the preceding code

                        . any character

                        * zero or more of the preceding code

                        $1 recall found group number one

                         

                        .. thus (you'll have to take my word for it) it puts all digits at the start of the file name into group one, gathers the rest, and replaces all of it with the contents of group #1 (which still contains the first digits, remember?). So the entire file name is replaced with just the digits in front.

                        If you are thinking "Hey, I can use this", you are not done here. This GREP operation returns a string type, and to convert it to a number you have to tell JS to do so:

                         

                        n = Number(n);

                         

                        .. and now you can add 1 to it the usual way. It's also possible your file accidentally does not start with one or more digits, and in that case the variable 'n' will be empty, so you have to test for n = '' before converting to a number.

                         

                        The GREP is smarter, because it can find any number of digits, which would be a much more difficult task with plain string manipulations, and it automatically rejects (i.e., stops at) the first non-digit.

                        • 9. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                          [Jongware] Most Valuable Participant

                          (Replying -- or maybe just talking -- to myself:)

                           

                          Hence the 'typeof' check -- which, incidentally, I didn't know, but I can guess at what it does. I bet it returns 'true' if the variable contains a number, 'false' if not; and NaN is per definition not a number.

                           

                          Apparently, 'typeof' returns the type of the argument as a string, so all you have to do is check if

                           

                          typeof(x) != 'number'

                           

                          returning false if it's not a number (as != stands for "is not equal to"). So if x is a variable of type 'whatchamacallit', this does not equal the string 'number'.

                          It's not anywhere in the Javascript Help that comes with InDesign, so it must be a built-in command like if, while, else, return, and so on.

                          • 10. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                            Shonkyin Level 3

                            Hi Danny,

                             

                            First of all i want say that all credit goes to Kasyan, because he done all the script and i have just added few code.

                             

                            And regarding syntax, Jongware already explain each and everything in very beautiful way.

                             

                            I am agree with Jongware that Grep is smarter way to do this. Now you have choice in your script 'Grep' and 'substr()'.

                             

                            Thanks Jongware for explaining each and everything.

                             

                            Me and many other learning from you and other scripting GURU's.

                             

                             

                            Shonky

                            • 11. Re: Naming PDF at export using a portion of IDCS4 File name(JS)
                              Danny Noonan Level 1

                              Jongware,

                              Thanks for taking the time to thoroughly explain your thoughts on this. I actually understand your explanation! I am going to experiment a bit with the GREP. This could be very useful in MANY of our other workflows. A new direction I can explore, I love this stuff.

                               

                              Oh yeah, I do have your version of the OMV and it is much easier to work with than ESTK's. . .

                               

                              Thanks again Jongware, your help is greatly appreciated.

                               

                              Danny