5 Replies Latest reply on Nov 14, 2017 6:22 PM by lan.mine

    CSV for Illustrator with multiple files

    lan.mine

      I am very much a new to scripting. I was given a task to see why this script was working for CS6 but not for CC2014. Can the mighty experts please help me with this one.

       

      Steps:

       

      • Open PDF files from folder (XS, S, L, etc.)
      • Match Size from XML to Layer Name. (Maybe have it match to file name instead?)
      • Match Placement from XML to a Text Object with the same Placement name

       

      Challenging part is one CSV file for multiple Illustrator files.

       

       

       

      Here is what I have so far. It was working CS6 but it stop working in CC. It is currently creating text files but the text work change.

       

      #target Illustrator
      
      //////////////////////////////////////////////////////////////////////////////////Main Code
      
      var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
      
      //sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator .ai files you want to convert to PDF');// Select the source folder.
      
      //setting the only folder this is going work out of is the Patterns Folder
      sourceFolder = Folder("~/Desktop/Patterns");
      
      if ( sourceFolder != null ){// If a valid folder is selected
          files = new Array();
          files = sourceFolder.getFiles();
      
          if ( files.length > 0 ){
              
              fileType = "*.pdf"; //prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
              files = sourceFolder.getFiles( fileType );// Get all files matching the pattern
              //destFolder = sourceFolder;
              
              for ( i = 0; i < files.length; i++ ){
                  sourceDoc = app.open(files[i]); // returns the document object
                  }
      
      
      
      var maxTxtWdth = 100000
      var sizeCounter =0
      var garmentCounter =0
      var placementCounter =0  //the number of placements, per garment,  found in the fileArray
      var garmentData
      var placementArray  =new Array()//contains the placement+content info, per placement, found in fileArray
      var aveTxtObj//the number of textbojects per art board
      var txtWdthArray = new Array()//use to store the max width of each text object
      var txtKernArray =new Array()//use to store the default kerning value
      
      if(app.documents.length > 0){//Check for active document
          var doc = app.activeDocument
          
          importList()
          getMaxTxtWdth()
          getNumberOfLines()
      
      
      }
      else{
              alert('please open template')
              }
          }
      }
      
      //////////////////////////////////////////////////////////////////////////////////Functions for Import List
      
      function importList(){  //If the template is sound, import CSV
          
        
              var csvFile = File.openDialog('Select a CSV File','comma-separated-values(*.csv):*.csv;');
              fileArray = readInCSV(csvFile)
              }
      
      
      
      function readInCSV(fileObj) {  //----Read CSV
          var fileArray = new Array();
          fileObj.open('r');
          fileObj.seek(0, 0);
          while(!fileObj.eof){
              var thisLine = fileObj.readln();
              var csvArray = thisLine.split(',');
              fileArray.push(csvArray);
              }
          fileObj.close();
          return fileArray;
          }
      
      
      
      function getTextData(dataFile){
        var df = dataFile;
        var dataFileName = decodeURI(df.name);
        var type = dataFileName.match(/(\.txt$|\.csv$)/i)[0].toLowerCase();
        var splitter = (type == '.txt')? '\t' : ',';
        df.open('r');
        var everyRowRaw = CSV.parse(df.read(), undefined, splitter);
        df.close();
        
        var everyRow = [];
        for(var i=0; i<everyRowRaw.length; i++){
          // get rid of empty rows
          var thisRawRow = everyRowRaw[i];
          if(!checkRowForAllBlanks(thisRawRow)){
            if(i > 0){
              if(thisRawRow.length < everyRow[0].length){
                var diff = everyRow[0].length - thisRawRow.length;
                for(var d=0; d<diff; d++){
                  thisRawRow.push("");
                }
              }
            }
            everyRow.push(thisRawRow);
          }
        }
        return everyRow;
      };
      
      function getData(filePath){
          try{
              return getTextData(File(filePath));
          } catch(e) {
              alert(e);
              return null;
          }
      };
      
      
      //////////////////////////////////////////////////////////////////////////////////Functions for Max Width Text
      
      
      function getMaxTxtWdth(){//Find intial max text width value, based on starting placeholder.
          for(i=0;doc.textFrames.length>i;i++){
      
              txtWdthArray[i] = doc.textFrames[i].width
               txtKernArray[i] = doc.textFrames[i].textRange.horizontalScale
               }
          }
      
      function getNumberOfLines(){
           for(i=1;fileArray.length;i++){//Set counter to 1 to skip the label(top) line
                 try{
                      if(fileArray[i][5].length!=0){//if a placement exists, start collecting info   //This line errors at end of array, causing the last entry to drop.  Consider complete rebuild
                          if(fileArray[i][2].length!=0){//check for size change
                             sizeCounter++
                              var garmentSize = fileArray[i][2]
                              }
                          if(fileArray[i][3].length!=0){//check for garment change
                              garmentCounter++
                              var garmentNumber = fileArray[i][3]
                              }
                          if(fileArray[i][4].length!=0){//check for number of placements
                              placementCounter++
                              }
                          }
                      else{//once the this current loop is satisfied, build the garment
                          for(x=0;placementCounter>x;x++){
                                 if(fileArray[i-x-1][4].split(';')[0].length!=0){
                                     placementArray[x] =  fileArray[i-x-1][4].split(';')[0]+':'+fileArray[i-x-1][5]
                                     }
                              }
      
                           buildGarmentV2(garmentCounter, garmentSize, placementCounter, placementArray)// this builds the indivdual garment
                          placementCounter=0
                          }
                      }// end try
                  
             catch(err){
                //alert('Error!    x='+x+' i='+i)
                //alert(garmentCounter+garmentSize+placementCounter+placementArray)
                 break
                  }//end catch
              }//end for loop
          }//end function getNumberOfLines
          
          
      
      function buildGarmentV2(garmentCounter, garmentSize, placementCounter, placementArray){//this will sort the AI doc and find corresponding placements based on Notes
          //alert(garmentCounter+garmentSize+ placementCounter+ placementArray)
          
          for(z=0;doc.textFrames.length>z;z++){
              if(doc.textFrames[z].note == garmentSize){//find each text frame with the correct size NOTE
                  //alert(garmentSize+ ' found at '+z)
                  for(y=0;placementArray.length>y;y++){
                      if(doc.textFrames[z].name == placementArray[y].split(':')[0]){
                          buildTextFrameV1(doc.textFrames[z], placementArray[y], z)
                          //alert(placementArray[y].split(':')[0]+ ' found at '+z)
                          break
                          }
                      }
                  }
              }
          
          redraw()
          
          
          
          
          exportPDF(garmentCounter, garmentSize, y)
          }
      
      
      function buildTextFrameV1(textFrame, placementContent, z){
          textFrame.contents = placementContent.split(':')[1]//assign contents to text frame
          doc.textFrames[z].textRange.horizontalScale = txtKernArray[z] // reset initial kerning value
          var temp = txtWdthArray[z]/textFrame.width //create new kerning value based on percentage of new value against placeholder
          if(textFrame.width > txtWdthArray[z]){//check  text width, if greater than initial char width, reduce
              if(txtKernArray[z]<(temp*100)){
                  doc.textFrames[z].textRange.horizontalScale = txtKernArray[z]
                  }
              else if(txtKernArray[z]>(temp*100)){
                  doc.textFrames[z].textRange.horizontalScale =temp*100
                  }
              }
          }
      
      function expandText(){
          var docRef = app.activeDocument;
          var layers = docRef.layers;
      
          for(var a=0;a<layers.length;a++){
              var thisLayer = layers[a];
              
              if(thisLayer.locked){
                  thisLayer.locked = false;
                  }
      
              if(!thisLayer.visible){
                  thisLayer.visible = true
                  }
              }
          app.executeMenuCommand("Text Objects menu item");
          app.executeMenuCommand("outline");
          }
        
      
      
      function exportPDF(garmentCounter, garmentSize){
          var saveOptions = new PDFSaveOptions()
          var theFile = new File("~\\Desktop\\Patterns\\"+garmentSize+garmentCounter+'.pdf')
          var flatOpts = new PrintFlattenerOptions();
          saveOptions.saveMultipleArtboards = true
          saveOptions.preserveEditability = true
          saveOptions.flattenerOptions = flatOpts
          flatOpts.convertTextToOutlines = true//this does nothing?
          app.activeDocument.saveAs(theFile, saveOptions);
          }
      
      
      alert ("Finished");
      
        • 1. Re: CSV for Illustrator with multiple files
          Silly-V Adobe Community Professional

          I see some of my code in here - it shouldn't work as is, because your Andy VanWaggoner's CSV parser is not present in this code.

          Maybe someone ran some other code on your CS6 which instituted the CSV global object, but it's only on that machine?

          Well, just kidding you are not actually using it, but who knows - what error does it give you?

          • 2. Re: CSV for Illustrator with multiple files
            lan.mine Level 1

            Its not giving me an error. But it's also not replacing the text like it is supposed to. The script is counting the garmentSize and the garmentCounter because of the pdf creation. So my assumption is that for some reason the script is not recognizing and matching placement info from the CSV to Illustrator? (Also highlighted are the original pdf that exist in the folder before running the script.)

             

            • 3. Re: CSV for Illustrator with multiple files
              lan.mine Level 1

              I just activated the alert below and this is what pops on the screen. The alert is showing that it is not recognizing the placement array. Any ideas on this one?

               

               

               

               

               

              function getNumberOfLines(){
                   for(i=1;fileArray.length;i++){//Set counter to 1 to skip the label(top) line
                         try{
                              if(fileArray[i][5].length!=0){//if a placement exists, start collecting info   //This line errors at end of array, causing the last entry to drop.  Consider complete rebuild
                                  if(fileArray[i][2].length!=0){//check for size change
                                     sizeCounter++
                                      var garmentSize = fileArray[i][2]
                                      }
                                  if(fileArray[i][3].length!=0){//check for garment change
                                      garmentCounter++
                                      var garmentNumber = fileArray[i][3]
                                      }
                                  if(fileArray[i][4].length!=0){//check for number of placements
                                      placementCounter++
                                      }
                                  }
                              else{//once the this current loop is satisfied, build the garment
                                  for(x=0;placementCounter>x;x++){
                                         if(fileArray[i-x-1][4].split(';')[0].length!=0){
                                             placementArray[x] =  fileArray[i-x-1][4].split(';')[0]+':'+fileArray[i-x-1][5]
                                             }
                                      }
              
                                   buildGarmentV2(garmentCounter, garmentSize, placementCounter, placementArray)// this builds the indivdual garment
                                  placementCounter=0
                                  }
                              }// end try
                          
                     catch(err){
                        alert('Error!    x='+x+' i='+i)
                        alert(garmentCounter+garmentSize+placementCounter+placementArray)
                         break
                          }//end catch
                      }//end for loop
                  }//end function getNumberOfLines
              
              • 4. Re: CSV for Illustrator with multiple files
                lan.mine Level 1

                Actually, I was wrong. It's calling the placement arrays like it is supposed to. Sorry, I am slowly learning a bit more about this script.

                • 5. Re: CSV for Illustrator with multiple files
                  lan.mine Level 1

                  Hi Silly-V,

                   

                  It looks like I have found the culprit. Very old code and I am finding easter eggs everyday. After some tweaking I got it to work. Thanks for checking this out.

                   

                  doc.textFrames[z].note