7 Replies Latest reply on Dec 15, 2011 7:03 AM by LEngelage

    Auto-naming pdfs from .csv 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;
      }