14 Replies Latest reply on Mar 2, 2012 1:30 PM by jffinn@mac.com

    Auto-naming pdfs from data file

    LEngelage

      I'm new to scripting but have a question. What script can I utilize to make use of one of the fields which will be (name) and use that to name the file when it is exported?

       

      I found this script but when I run it I get "null is not an object" from line 23, column 1.  I have the .csv file and folder named "pdfs" on my desktop. In the script I replaced "fichier.csv" with the name of my file. The first column in my .csv file is what I intend to rename the pdf files.

       

      var csv=File(Folder.desktop+"/fichier.csv");

      var myPDFFolder = Folder(Folder.desktop+"/pdfs");

      var names = getNamesFromCSV(csv);

      if(csv.exists && myPDFFolder.exists){
          var files = myPDFFolder.getFiles();
          if(files.length==names.length){
              for(var i=0; i<files.length; i++){
                  files[i].rename(files[i].parent+"/"+names[i]+".pdf");
              }
          }
      }
             
      function getNamesFromCSV(csv){
          if(csv.exists){
              csv.open('r');
              var content = csv.read();
              var namesArr = [];
              content=content.split("\n");
              for(var j=0; j<content.length; j++){
                  namesArr.push(content[j].match(/^(.[^\n]+)/i)[1]);
              }
                 
              csv.close();
              return namesArr.slice(1,namesArr.length);
          }
          return false;
      }

        • 1. Re: Auto-naming pdfs from data file
          absqua Level 4

          Hmm. There are a lot of issues here I think. Parsing a csv can be tricky.

           

          You're getting an error because the regular expression you're feeding to .match() isn't matching anything in one of your content members. This is probably the last one—probably there was a newline at the end of the csv, so the last member is empty.

           

          A couple of other things: you're splitting the content of the csv on newlines, but not on commas or tabs or any other field delimiter. So, each file will get named to the entire record, rather than the first field in each record. I doubt this is what you want. You're also stripping the first record out, which would work for a spreadsheet with headers, but not otherwise. Also, .rename() takes the display name, without path information, so your call to it will either fail or result in some very oddly named files.

           

          If you have control over generating the csv, I would recommend using a character odder than a comma or tab (perhaps a pipe or hash) as a field delimiter, and not putting quotes around the field. If you can post a sample of that, I can probably help you finish it.

           

          Or, if you are on a Mac and are comfortable on the command line, a little googling turned up this: http://systembash.com/content/one-line-batch-rename-files-using-csv-input-file-and-awk

           

          Jeff

          • 2. Re: Auto-naming pdfs from data file
            csm_phil Level 4

            Hi LEngelage,

             

            I have modified your JS code, I hope this what you expect. Else you revert back!

             

            Your input CSV file like this 1snapshot in below screenshot another snapshot is changed the file name in the Desktop folder of "~/Desktop/1".

            Picture 1.png     Picture 2.png

             

             

            var csv=File(Folder.desktop+"/Workbook1.csv");
            var myPDFFolder = Folder("~/Desktop/1/");
                
            if(csv.exists){
                 csv.open('r');
                 var content = csv.read();
                 var namesArr = [];
                 content=content.split("\n");
                 for(var j=0; j<content.length; j++){
                      var mySplit = content[j].split(",");
                      var myF = mySplit[0];
                      var myS = mySplit[1];
                      var files = myPDFFolder.getFiles();
                      for(var i=0; i<files.length; i++){
                           var myOrg = files[i].fullName.split("/").pop();
                           if(mySplit[0]===myOrg){
                                files[i].rename(mySplit[1]+".pdf");
                                }
                           }
                      }
                 csv.close();
                 }
            

             

             

            thx

            csm_phil

            • 3. Re: Auto-naming pdfs from data file
              LEngelage Level 1

              Your code worked absolutely great. Thank you so much! It was exactly what I needed. Just hope it runs on thousands of file names. Kudos to you.

              • 4. Re: Auto-naming pdfs from data file
                jffinn@mac.com Level 1

                Hi csm_phil,

                I know this post is a little bit old, but  saw the string of the conversation. I am new to scripting, but can you please explain what is going on in layman's terms?

                 

                I figured out the data merge feature in InDesign to generate a file with all of the data from the CSM file. I can generate a batch of data merged InDesign files and then I am stuck spliting the huge pdf into smaller 2-page pdfs. Assuming that I came up with an easy way to name the files, would this script work to auto-name the pdfs from the CSV file? This is exactly what I am hoping to do — take a bunch of files and find out a way to automate the process. Bridge doesn't work here, because the numbers don't always follow a sequential order.

                 

                If I saved all of the files from the data merge in a folder called files_to_convert and then made a folder called finished files can I run this script and changed the text in red?

                 

                And the big question is... where do I insert this script? In InDesign or Automator??

                 

                Thank you in advance,

                jffinn

                • 5. Re: Auto-naming pdfs from data file
                  John Hawkinson Level 5

                  And the big question is... where do I insert this script? In InDesign or Automator??

                  This is a JavaScript, it goes in your InDesign scripts folder with a .jsx extension.

                  • 6. Re: Auto-naming pdfs from data file
                    jffinn@mac.com Level 1

                    Thanks so much. I just tried running the JS in InDesign CS 4 and CS 5.5 on Mac OS X (10.6) and I am getting a JS error.

                    I changed the file name to Workbook1JF.csv and changed the folder name from 1 to FilestoConvert and updated the script.

                     

                    var csv=File(Folder.desktop+"/Workbook1JF.csv");

                    var myPDFFolder = Folder("~/Desktop/FilestoConvert/");

                     

                    Does the CSV file need to reside somewhere? In the folder FilestoConvert??


                    I keep getting this error message:

                    Screen shot 2012-02-21 at 3.00.18 PM.png

                    Do I need to do anything else to enable the script to work?

                     

                    Thank you,

                    Jffinn

                    • 7. Re: Auto-naming pdfs from data file
                      Larry G. Schneider Adobe Community Professional & MVP

                      You will need to go back to what ever text editor you used and change the file to a plain text file. The error message indicates you saved it as an RTF file which won;t work.

                      • 8. Re: Auto-naming pdfs from data file
                        LEngelage Level 1

                        Let me explain what I did:

                         

                        I used ExtendScript Toolkit CS5.5 (just double clicked the .jsx file). You can open ExtendScript Toolkit and copy this script into the program and save.

                         

                        Here's the script I modified. You will need a folder on your DESKTOP named "RenameFolder" and a .csv file named "Renaming.csv". In your .csv file you need two columns. Column A is the current name of each of your pdf files, the second will be the name for each file to change to minus the extension. I used Adobe Acrobat to extract the pages in the pdf to multiple files using the "extract each page as separate" menu and put these into my "RenameFolder" Once this is set up you should be able to run the script through ExtendScript Toolkit by clicking the run button.

                         

                        var csv=File(Folder.desktop+"/Renaming.csv");

                        var myPDFFolder = Folder("~/Desktop/RenameFolder/");

                             

                        if(csv.exists){

                             csv.open('r');

                             var content = csv.read();

                             var namesArr = [];

                             content=content.split("\n");

                             for(var j=0; j<content.length; j++){

                                  var mySplit = content[j].split(",");

                                  var myF = mySplit[0];

                                  var myS = mySplit[1];

                                  var files = myPDFFolder.getFiles();

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

                                       var myOrg = files[i].fullName.split("/").pop();

                                       if(mySplit[0]===myOrg){

                                            files[i].rename(mySplit[1]+".pdf");

                                            }

                                       }

                                  }

                             csv.close();

                             }

                         

                        Hope this is helpful.

                        Lynda

                        • 9. Re: Auto-naming pdfs from data file
                          jffinn@mac.com Level 1

                          Hi LInda,

                          This is great info. Thank you so much. I opened script editor, pasted the script in, made the changes and I ran the script about 3-4 times and I received a value=true notice, but I didn't aee any change happening in the folder. I renamed the file, updated the script and now I am receiving an result undefined notice. I took a screen shot. I named the file "WorkbookJF2.csv", the folder "Convert" and placed the 2 pdf in the folder on my desktop. The conversion should take place in the same folder as the pdfs.

                           

                          montage.png

                           

                          I think I am really close. Do you have any idea if I am missing something.

                           

                          Thanks for your patience (I am a graphic designer by trade and not a programmer, so this is all new to me.),

                          Jffinn (Jonathan)

                          • 10. Re: Auto-naming pdfs from data file
                            LEngelage Level 1

                            I'm not a programmer either but figured it out by trial and error. If you were getting a "true" notice, I would think it should have worked. I do think you need the two separate folders and they have to be on your desktop. The names on the folders have to be exactly the same as in the script, case sensitive included. Other than that, I don't know.

                            • 11. Re: Auto-naming pdfs from data file
                              jffinn@mac.com Level 1

                              Hi Lynda,

                              May I run 2 more things by you? I am trying to match your workflow so that all items are in the correct folder, etc.

                              I am running the script on the desktop "Source1jf.jsx"

                              In a folder, called "convert", I have the .csv file "workbook2.csv" and the 2 pdf files that I am going to convert.

                               

                              var csv=File(Folder.desktop+"/workbook2.csv");

                              var myPDFFolder = Folder("~/Desktop/convert/");

                               

                              I copied/pasted the script you provided and I am just not sure of something. What does the 2nd line of the script refr to? Does it refer to the folder where the original pdfs are, or does it refer to the folder where the pdfs are going?? I am not ure if I should change line 2 of your script to

                               

                              var myPDFFolder = Folder("~/Desktop/convert/"); or var myPDFFolder = Folder("~/Desktop/finished/");

                               

                              I am so close....

                               

                              Thanks,

                              Jonathan

                              • 12. Re: Auto-naming pdfs from data file
                                LEngelage Level 1

                                The second line in the script (I believe) is referring to the folder that your files to be converted are in. When the script runs you should see them change to the name that you have in column "B" in the .csv file. The .csv file should be on the desktop (not in a folder). So you should have on your desktop: A folder named "convert" with your files to be renamed in it and also the workbook2.csv file not in a folder on your desktop. See if this works.

                                • 13. Re: Auto-naming pdfs from data file
                                  jffinn@mac.com Level 1

                                  Hi Lynda,

                                  Thank you so much... I wil try this solution tonight.

                                   

                                  Thanks,

                                  JF

                                  • 14. Re: Auto-naming pdfs from data file
                                    jffinn@mac.com Level 1

                                    I just wanted to let you knw that I got the script to work. The issue was the location of the .csv file. The file has to reside outside of the convert folder and should be open.

                                     

                                    Thanks,

                                    JF