7 Replies Latest reply on Jul 25, 2017 8:00 PM by julioc4816867

    i need help with Convert AI script to Indesign

    julioc4816867

      I have 10000 indesing files, and I need replace only specific one text, all files has the same layer name:

       

      123123.png

       

      Ai script

      anyone can give me some help please?

       

      function import_csv_and_save(){  
          var myCSV = File.openDialog('select TXT...','file TXT:*.txt',false)||'';    
          if(myCSV.exists == null){
              return;    
          }
          var csvCount = 0;
          var myARRAY = read_CSV();
          alert(csvCount + " line text");    
      var layerName = prompt('layer name \'Layer\':', 'write here...', 'specific text layer ');
      if(layerName == null){    
              return;    
          }
          var myFOLDER = Folder.selectDialog('select source...')||'';    
          if(myFOLDER.exists == null){    
              return;    
          }
          myDEST = Folder.selectDialog('Save folder...')||'';    
          if(myDEST.exists == null){    
              return;    
          }    
          var illustratorSaveOpts = new IllustratorSaveOptions();      
          illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR17;    
          illustratorSaveOpts.compressed = true; // Version 10+    
          illustratorSaveOpts.pdfCompatible = true;  // Version 10+    
          illustratorSaveOpts.embedICCProfile = true;  // Version 9+    
          illustratorSaveOpts.embedLinkedFiles = false;  // Version 7+        
          
          
          var myFILES = myFOLDER.getFiles('*.ai');    
          var myTEXT, testtext, newNAME, fC = 0;    
          for(var h = 0; h < myFILES.length; h++){    
              myNAME = myFILES[h].name.replace('.ai','');    
              app.open(myFILES[h]);    
              if(app.activeDocument.pageItems.getByName(layerName) == 0){    
                  alert("TextFrame named is not available in the activeDocument");    
                  return;    
              }    
              myTEXT = app.activeDocument.pageItems.getByName(layerName);    
              testtext = myTEXT.contents;    
              for(var i = 0; i < myARRAY.length; i++){    
                  if(myARRAY[i][0] === myNAME){   
                      // test for multiline text  - - can deal with 1, 2, or 9 lines.  
                      if(myARRAY[i][1].substr(0,1) === '"'){  
                          if(myARRAY[i+1][0].substr(myARRAY[i+1][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0].substr(0,myARRAY[i+1][0].length-1);  
                          }else if(myARRAY[i+2][0].substr(myARRAY[i+2][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0].substr(0,myARRAY[i+2][0].length-1);  
                          }else if(myARRAY[i+3][0].substr(myARRAY[i+3][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0].substr(0,myARRAY[i+3][0].length-1);  
                          }else if(myARRAY[i+4][0].substr(myARRAY[i+4][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0] + "\n" + myARRAY[i+4][0].substr(0,myARRAY[i+4][0].length-1);  
                          }else if(myARRAY[i+5][0].substr(myARRAY[i+5][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0] + "\n" + myARRAY[i+4][0] + "\n" + myARRAY[i+5][0].substr(0,myARRAY[i+5][0].length-1);  
                          }else if(myARRAY[i+6][0].substr(myARRAY[i+6][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0] + "\n" + myARRAY[i+4][0] + "\n" + myARRAY[i+5][0] + "\n"+ myARRAY[i+6][0].substr(0,myARRAY[i+6][0].length-1);  
                          }else if(myARRAY[i+7][0].substr(myARRAY[i+7][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0] + "\n" + myARRAY[i+4][0] + "\n" + myARRAY[i+5][0] + "\n" + myARRAY[i+6][0] + "\n" + myARRAY[i+7][0].substr(0,myARRAY[i+7][0].length-1);  
                          }else if(myARRAY[i+8][0].substr(myARRAY[i+8][0].length-1,1) === '"'){  
                              myTEXT.contents = myARRAY[i][1].substr(1) + "\n" + myARRAY[i+1][0]  + "\n" + myARRAY[i+2][0] + "\n" + myARRAY[i+3][0] + "\n" + myARRAY[i+4][0] + "\n" + myARRAY[i+5][0] + "\n" + myARRAY[i+6][0] + "\n" + myARRAY[i+7][0] + "\n"+ myARRAY[i+8][0].substr(0,myARRAY[i+8][0].length-1);  
                          }
      }else{  
                          myTEXT.contents = myARRAY[i][1];   
                      }  
                      newFILE = new File(myDEST + "/" + myFILES[h].name);    
                      app.activeDocument.saveAs(newFILE, illustratorSaveOpts);    
                       fC++    
                      break;    
                  }    
              }    
              if(myTEXT.contents === testtext){    
                  alert("processing of " + myFILES[h].name + " failed, Text was not changed.");    
              }    
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);    
          }    
          alert("Saved " + fC + " Files, into " + myDEST);    
          
          
          function read_CSV(){    
              var myARRAY = [], line, lineArray;    
              myCSV.open('r');    
              while(!myCSV.eof){    
                  line = myCSV.readln();    
                  lineArray = line.split('\t');    
                  myARRAY.push(lineArray);    
                  csvCount++    
              }    
              myCSV.close();    
              return myARRAY;    
          }    
      }
      
      

       

      Script indesign:

       

       var main = function() {
         // check for active document
         if (app.documents.length < 1) {
         // no doc
         return;
         } else {
         // sleect the file
         var file = File.openDialog("Select a textfile", "*.*", false);
         if (file === null) {
         // aborted by user
         return;
         }
         // read in the file line per line
         var lines = [];
        file.open('r');
         while (!file.eof) {
        lines[lines.length] = file.readln();
         }
        file.close(); // always close files
      
         var doc = app.activeDocument;
         // set the origin to the page
         var curr_origin = doc.viewPreferences.rulerOrigin;
        doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
      
         // the locations for the textframe
         var y1 = 0;
         var x1 = 0;
         var y2 = 100;
         var x2 = 100;
      
         // loop all pages
         for (var i = 0; i < doc.pages.length; i++) {
         var page = doc.pages[i];
         // get the content
         var content = "not enough lines";
         if (lines.length > i) {
         // we still have lines in the file
        content = lines[i];
         }
         // create the textframe
         var tf = page.textFrames.add({
        contents: content,
        geometricBounds: [y1, x1, y2, x2]
         });
         // apply a style by name
         // tf.paragraphs[0].appliedParagraphStyle = doc.paragraphStyles.item("myStyle");
         }
        doc.viewPreferences.rulerOrigin = curr_origin;
      
         }
      
        };
        main();