10 Replies Latest reply on Dec 16, 2017 9:46 AM by renél80416020

    batch find & replace text in Illustrator files in a folder?

    susank90600475

      I found this script which work greats on any currently open Illustrator files, but I would like it to be able to run the action on a Mac OS folder, open the first Illustrator, runs this javascript, save & close that file, then loop through all other Illustrator files in a folder and repeat the process, until the last file.

       

      1. function FindAndReplaceScript_AllOpenDocuments(){     
      2.     for(var i=app.documents.length -1; i > -1;  i--){     
      3.         app.documents[i].activate(); 
      4.         var aDoc = app.documents[i]; 
      5.              
      6.         var searchString = /VT/gi;    
      7.         var replaceString = ‘VHB';      
      8.              
      9.         var theTF = aDoc.textFrames;     
      10.         if (theTF.length > 0) {     
      11.             for (var j = 0 ; j <theTF.length; j++) {     
      12.                 var aTF = theTF[j];     
      13.                 var newString = aTF.contents.replace(searchString, replaceString);     
      14.                 if (newString != aTF.contents) {     
      15.                     theTF[j].contents = newString;     
      16.                 }     
      17.             }     
      18.         } 
      19.     } 
      20. };     
      21. FindAndReplaceScript_AllOpenDocuments(); 
        • 1. Re: batch find & replace text in Illustrator files in a folder?
          OMOTI Level 3

          Hello.

          Using AppleScript or getting folder by "Folder.selectDialog" via ExtendScript will work great.

          • 2. Re: batch find & replace text in Illustrator files in a folder?
            susank90600475 Level 1

            So I'm really new to all of this, but I'm assuming I would have to use Apple script to apply an action to a folder in Mac OS, go to the first file that is an Illustrator file, open it, then run the javascript on that file, save the change, close it, then jump back to the AppleScript to go to the next found Illustrator file and do it over again, looping through till all Illustrator files within that folder have had the change made.

            I'm just not so sure how to do the AppleScript part. Any help in this area?

            • 3. Re: batch find & replace text in Illustrator files in a folder?
              williamadowling Level 4

              You can do all of this natively in JS. there's no reason to invoke applescript.

              • 4. Re: batch find & replace text in Illustrator files in a folder?
                OMOTI Level 3

                To my knowledge, AppleScript makes it easier if you want to run a script with folder actions like "droplet". Otherwise, JS is easy.

                • 5. Re: batch find & replace text in Illustrator files in a folder?
                  williamadowling Level 4

                  perhaps i misunderstood what was actually needed.

                   

                  Susan, did you imagine running a script that would loop through the files of a folder upon execution? Or are you looking for a script that will watch the folder for files being added, and then run the script on the newly added files?

                   

                  The former can easily be done in JS alone. However, the latter gets much deeper into the weeds with regards to cross-language coordination.

                  • 6. Re: batch find & replace text in Illustrator files in a folder?
                    susank90600475 Level 1

                    Yes, williamadowling, the first opt. I want to execute a loop through the files of a folder upon execution.

                    • 7. Re: batch find & replace text in Illustrator files in a folder?
                      OMOTI Level 3

                      Run the script in Illustrator.

                      Select a folder in the open file reference dialog specific to the embedded platform.
                      After that, it automatically repeats that it opens the AI file in the folder, replaces the character string and closes it.
                      Like this?

                       

                      Document(pdf) "JavaScript Tools Guide" will help for you.

                       

                      Tips about coding.

                       

                      // Opens the built-in platform-specific file-browsing dialog.
                      // If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.
                      var folderObject = Folder.selectDialog("select Folder");

                       

                      // Gets files in the folder.
                      var fileObjectList = folderObject.getFiles();

                       

                      // Opens the file
                      for (var i = 0; i < fileObjectList.length; i++) {
                          app.open(fileObjectList[i]);
                      }

                       

                       

                      • 8. Re: batch find & replace text in Illustrator files in a folder?
                        susank90600475 Level 1

                        Hi OMOTI, thanks for the help.

                        I made the change to the script but now get 2 errors:

                        Screen Shot 2017-12-11 at 2.47.06 PM.pngScreen Shot 2017-12-11 at 2.47.16 PM.png

                         

                        Here is the script as it currently stands:

                         

                        // Opens the built-in platform-specific file-browsing dialog.

                        // If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.

                        var folderObject = Folder.selectDialog("select Folder");

                         

                        // Gets files in the folder.

                        var fileObjectList = folderObject.getFiles();

                         

                        // Opens the file

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

                            app.open(fileObjectList[i]);

                        }function FindAndReplaceScript_AllOpenDocuments(){     

                            for(var i=app.documents.length -1; i > -1;  i--){     

                                app.documents[i].activate(); 

                                var aDoc = app.documents[i]; 

                                     

                                var searchString = /VT/gi;    

                                var replaceString = " VHB " ;

                                     

                                var theTF = aDoc.textFrames;     

                                if (theTF.length > 0) {     

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

                                        var aTF = theTF[j];     

                                        var newString = aTF.contents.replace(searchString, replaceString);     

                                        if (newString != aTF.contents) {     

                                            theTF[j].contents = newString;     

                                        }     

                                    }     

                                } 

                            } 

                        };     

                        FindAndReplaceScript_AllOpenDocuments(); 

                        • 9. Re: batch find & replace text in Illustrator files in a folder?
                          williamadowling Level 4

                          try this:

                           

                          // Opens the built-in platform-specific file-browsing dialog.

                          // If the user clicks OK, returns a File or Folder object for the selected file or folder. If the user cancels, returns null.

                          var folderObject = Folder.selectDialog("select Folder");

                           

                          // Gets files in the folder.

                          var fileObjectList = folderObject.getFiles();

                           

                          //regex to check that the file is an .ai file

                          var filePat = /\.ai$/;

                           

                           

                          // Opens the file

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

                              if(filePat.test(fileObjectList[i].name))

                              {

                                  continue;

                              }

                              app.open(fileObjectList[i]);

                          }function FindAndReplaceScript_AllOpenDocuments(){    

                              for(var i=app.documents.length -1; i > -1;  i--){

                           

                           

                                  app.documents[i].activate();

                                  var aDoc = app.documents[i];

                                      

                                  var searchString = /VT/gi;   

                                  var replaceString = " VHB " ;

                                      

                                  var theTF = aDoc.textFrames;    

                                  if (theTF.length > 0) {    

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

                                          var aTF = theTF[j];    

                                          var newString = aTF.contents.replace(searchString, replaceString);    

                                          if (newString != aTF.contents) {    

                                              theTF[j].contents = newString;    

                                          }    

                                      }    

                                  }

                              }

                          };    

                          FindAndReplaceScript_AllOpenDocuments();

                          • 10. Re: batch find & replace text in Illustrator files in a folder?
                            renél80416020 Level 3

                            Bonjour,

                            Je vais au plus simple (sans les options d'ouverture et de sauvegarde)

                            //----------------------------------------------
                            app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                            // Gets files in the folder.
                            var folderObject = Folder.selectDialog("select Folder");
                            var fileObjectList = folderObject.getFiles("*.ai");
                            var docRef;
                            for (var i = 0; i < fileObjectList.length; i++) {
                                docRef = app.open(fileObjectList[i]);
                                FindAndReplaceScript_OpenDocuments(docRef);
                                docRef.saveAs (fileObjectList[i]);
                                docRef.close(SaveOptions.DONOTSAVECHANGES);
                            }
                            /-----------------------------------------
                            function FindAndReplaceScript_OpenDocuments(aDoc){
                                   var searchString = /VT/gi;
                                   var replaceString = "VHB";
                                    var theTF = aDoc.textFrames;
                                    if (theTF.length > 0) {
                                        for (var j = 0 ; j <theTF.length; j++) {
                                            var aTF = theTF[j];
                                            var newString = aTF.contents.replace(searchString, replaceString);
                                            if (newString != aTF.contents) {
                                               theTF[j].contents = newString;
                                            }
                                       }
                                    }
                            }