27 Replies Latest reply on May 18, 2018 1:02 AM by penkat83858457

    Find and Replace Script for multiple files

    jasonm64137335 Level 1

      I am trying to find a script that will either open a selection of .ai files or run through all the open files in illustrator and complete a Find and Replace. Right now I have a script that runs the Find and Replace but I have to run it on each document instead of all my open files.

       

      var openDocument = app.documents.item(0);
        
      var search_string = /001D/gi; // g for global search, remove i to make a case sensitive search  
      var replace_string = '001E';  
        
      var text_frames = active_doc.textFrames;  
        
      if (text_frames.length > 0)  
      {  
          for (var i = 0 ; i < text_frames.length; i++)  
            {  
                var this_text_frame = text_frames[i];  
                 var new_string = this_text_frame.contents.replace(search_string, replace_string);  
                   
                 if (new_string != this_text_frame.contents)  
                     {  
                          this_text_frame.contents = new_string;  
                     }  
            }  
      }
      

       

      Thanks for any help.

        • 1. Re: Find and Replace Script for multiple files
          try67 MVP & Adobe Community Professional

          [Moved to the Illustrator Scripting forum]

          • 2. Re: Find and Replace Script for multiple files
            Silly-V Adobe Community Professional

            If you care to use Actions, you may stick this script into an action and then batch-process a folder of files with that action.

            But, I'm not sure your script will work since your active document is called "openDocument" in one line and "active_doc" in another line.

            • 3. Re: Find and Replace Script for multiple files
              pixxxel schubser MVP & Adobe Community Professional

              Hi jasonm64137335,

              you can do something like this:

              var aDoc = app.activeDocument;
              aDoc.textFrames[0].contents = "abcdABCD"; // this is only for one seachable text
              
              var searchString = /\u0041/gi; // search for "a" or "A"
              var replaceString = "\u0058"; // replace with "X"
              
              var theTF = aDoc.textFrames;
              if (theTF.length > 0) {
                  for ( i = 0 ; i <=theTF.length-1; i++) {
                      var aTF = theTF[i];
                      var newString = aTF.contents.replace(searchString, replaceString);
                      if (newString != aTF.contents) {
                          theTF[i].contents = newString;
                          }
                      }
                  }
              

               

              Have fun

               

              • 4. Re: Find and Replace Script for multiple files
                jasonm64137335 Level 1

                This works but still only replaces it in one open Document, I would like it to run through all open documents if possible.

                • 5. Re: Find and Replace Script for multiple files
                  Silly-V Adobe Community Professional

                  Then you must wrap pixxxel's code in this code:

                   

                  function FindAndReplaceScript_AllOpenDocuments(){
                      for(var i=0; i<app.documents.length; i++){
                            app.documents[i].activate();
                            // paste pixxxel's code starting here on this line
                      };
                  };
                  FindAndReplaceScript_AllOpenDocuments();
                  
                  • 6. Re: Find and Replace Script for multiple files
                    Vinicius Baptista Level 2

                    Are you tried use app.documents[]? like this:

                     

                     

                    for (x=0; x < app.documents.length; x++){
                    
                    
                    app.documents[x].activate();
                    
                    
                    var aDoc = app.activeDocument;  
                    aDoc.textFrames[0].contents = "abcdABCD"; // this is only for one seachable text  
                      
                    var searchString = /\u0041/gi; // search for "a" or "A"  
                    var replaceString = "\u0058"; // replace with "X"  
                      
                    var theTF = aDoc.textFrames;  
                    if (theTF.length > 0) {  
                        for ( i = 0 ; i <=theTF.length-1; i++) {  
                            var aTF = theTF[i];  
                            var newString = aTF.contents.replace(searchString, replaceString);  
                            if (newString != aTF.contents) {  
                                theTF[i].contents = newString;  
                                }  
                            }  
                        }  
                    
                    
                    
                    
                    };
                    
                    

                     

                     

                    I think it work

                     

                    Greetings

                    -Vinícius

                    • 7. Re: Find and Replace Script for multiple files
                      jasonm64137335 Level 1

                      So this is what I have now:

                      function FindAndReplaceScript_AllOpenDocuments(){  
                          for(var i=0; i<app.documents.length; i++){  
                                app.documents[i].activate();  
                      
                      
                      var aDoc = app.activeDocument;  
                      aDoc.textFrames[0].contents = "abcdABCD"; // this is only for one seachable text  
                        
                      var searchString = /001D/gi; 
                      var replaceString = '001E';   
                        
                      var theTF = aDoc.textFrames;  
                      if (theTF.length > 0) {  
                          for ( i = 0 ; i <=theTF.length-1; i++) {  
                              var aTF = theTF[i];  
                              var newString = aTF.contents.replace(searchString, replaceString);  
                              if (newString != aTF.contents) {  
                                  theTF[i].contents = newString;  
                                  }  
                              }  
                          }  
                      };  
                      };  
                      FindAndReplaceScript_AllOpenDocuments();
                      
                      
                      
                      
                      
                      
                      

                       

                      But it is still only changing one document of 21 that are open... I am over my head. Appreciate any help.

                      • 8. Re: Find and Replace Script for multiple files
                        Silly-V Adobe Community Professional

                        Oh, my bad, I gave bad advice, pasting pixxxel's code into my code results in a re-definition of the "i" counter.

                        Vinicius is corrent.

                         

                        Here is my updated one:

                         

                        function FindAndReplaceScript_AllOpenDocuments(){    
                            for(var i=app.documents.length -1; i > -1;  i--){    
                                app.documents[i].activate();
                                var aDoc = app.documents[i];
                                    
                                var searchString = /001D/gi;   
                                var replaceString = '001E';     
                                    
                                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(); 
                        
                        2 people found this helpful
                        • 9. Re: Find and Replace Script for multiple files
                          jasonm64137335 Level 1

                          Awesome, worked beautifully!

                          • 10. Re: Find and Replace Script for multiple files
                            ascotto Level 1

                            Hi guys, I was looking for a script like this, because I have lots of cruise maps whose locations are to be translated from IT to EN. I tried to run the script marked as correct answer, it returns no errors but it does…nothing. I mean, the window jitters a bit but anything else, might it be related to the 2015.5 update? Or am I missing some code, some steps?

                             

                            Thanks a lot!

                            • 11. Re: Find and Replace Script for multiple files
                              Silly-V Adobe Community Professional

                              Hello! Paste your code, let's see also a sample graphic of what you're trying to do.

                              • 12. Re: Find and Replace Script for multiple files
                                ascotto Level 1

                                Hi! I tried to run your code, marked as correct answer, and I simply need to find/replace city names on like 40 files, so that once i replace, e.g. "Venezia" with "Venice", it'll be replaced everywhere.

                                • 13. Re: Find and Replace Script for multiple files
                                  Silly-V Adobe Community Professional

                                  So you tried to run the code without making any edits to it?

                                  • 15. Re: Find and Replace Script for multiple files
                                    Crooty Level 1

                                    Same as ascotto here. I run the script and nothing happens. Is a dialog box supposed to appear?
                                    (also, I'm on CS6, should it work or not?)

                                     

                                    Thanks for your help.

                                    • 16. Re: Find and Replace Script for multiple files
                                      Silly-V Adobe Community Professional

                                      You have to edit the script to put in your words.

                                      These lines need to be edited to make it work. Change the 001D to your word you need to replace and the 001E to the one you want to change to.

                                       

                                      1.         var searchString = /001D/gi;    
                                      2.         var replaceString = '001E';  
                                      • 17. Re: Find and Replace Script for multiple files
                                        ascotto Level 1

                                        Hi Silly-V, thanks, it works fine Btw, if I try to replace two words instead of one ( eg: intead of '001E' I write 'hello world'), it returns me syntax error on that line. Why?

                                        • 18. Re: Find and Replace Script for multiple files
                                          Silly-V Adobe Community Professional

                                          It works fine for me, can you paste your line where you put in the "hello world" ?

                                          • 19. Re: Find and Replace Script for multiple files
                                            mordie

                                            Hi.

                                             

                                            Is there somewhere online where a simple Illustrator user like me can find out how to use your answer to do this?

                                             

                                            I have 320 files to revise and your wisdom would be most helpful.

                                             

                                            Thanks.

                                            • 20. Re: Find and Replace Script for multiple files
                                              penkat83858457 Level 1

                                              Hey guys, I've tried it, but it gives me a syntax error.

                                               

                                              Screen Shot 2018-05-17 at 10.12.53.png

                                               

                                              Do you have any idea why? Thanks!

                                              • 21. Re: Find and Replace Script for multiple files
                                                q3player Level 3

                                                I've tried out the script's last version from Silly-V using AI CS6 on Mac. Worked fine.

                                                While I ran the test I thought it might be useful to implement "save" and "close" in the script and maybe a dialog for selecting a folder containing the files to be done by the script instead of opening all of them at once. With 20 test files it took more time to save and close than running the script itself :-)

                                                • 22. Re: Find and Replace Script for multiple files
                                                  q3player Level 3
                                                  var searchString = /this text is wrong/gi;
                                                  // Put the text to be replaced between the two backslashes
                                                  
                                                  var replaceString = 'my new text which is correct';
                                                  // Put the replacing text between the apostrophes
                                                  
                                                  var dir = Folder.selectDialog ("Choose a folder containing .ai files.");
                                                  var files = dir.getFiles("*.ai");
                                                  
                                                  function FindAndReplaceScript_AllOpenDocuments(aDoc){      
                                                  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;
                                                  }}}};    
                                                  
                                                  for (var f = 0; f < files.length; f++) {
                                                  var doc = app.open(files[f]);
                                                  FindAndReplaceScript_AllOpenDocuments(doc); 
                                                  doc.close (SaveOptions.SAVECHANGES);
                                                  };
                                                  
                                                  

                                                   

                                                  Here you'll find an opening dialog and after doing the search and replace function the file will be saved and closed, going on to the next.

                                                  1 person found this helpful
                                                  • 23. Re: Find and Replace Script for multiple files
                                                    penkat83858457 Level 1

                                                    Hey, thanks for your input. This sounds like a great idea, to save and close. I guess, I'm not doing something right, because it gives me the same error message - I'm also using MAC - tried on CS6 as well. ;/

                                                    • 24. Re: Find and Replace Script for multiple files
                                                      q3player Level 3

                                                      Are you running the script from ESTK or from AI?

                                                      Maybe we should add this as first line:

                                                       

                                                      #target Illustrator

                                                      • 25. Re: Find and Replace Script for multiple files
                                                        q3player Level 3
                                                        #target Illustrator
                                                        
                                                        function getSearch () {
                                                        var myWindow = new Window ("dialog", "Search & Replace");
                                                            myWindow.alignChildren = "top";
                                                        var myInputGroup = myWindow.add ("group");
                                                            myInputGroup.add ("statictext", undefined, "Search for:");
                                                        var myText = myInputGroup.add ("edittext", undefined, "old");
                                                            myText.characters = 100;
                                                            myText.active = true;
                                                        var myButtonGroup = myWindow.add ("group");
                                                            myButtonGroup.alignment = "right";
                                                            myButtonGroup.add ("button", undefined, "OK");
                                                            myButtonGroup.add ("button", undefined, "Cancel");
                                                        myWindow.show ();
                                                        return (myText.text);
                                                        }
                                                        
                                                        function getReplace () {
                                                        var myWindow = new Window ("dialog", "Search & Replace");
                                                            myWindow.alignChildren = "top";
                                                        var myInputGroup = myWindow.add ("group");
                                                            myInputGroup.add ("statictext", undefined, "Replace with:");
                                                        var myText = myInputGroup.add ("edittext", undefined, "new");
                                                            myText.characters = 100;
                                                            myText.active = true;
                                                        var myButtonGroup = myWindow.add ("group");
                                                            myButtonGroup.alignment = "right";
                                                            myButtonGroup.add ("button", undefined, "OK");
                                                            myButtonGroup.add ("button", undefined, "Cancel");
                                                        myWindow.show ();
                                                        return (myText.text);
                                                        }
                                                        
                                                        var searchString = getSearch();
                                                        var replaceString = getReplace();
                                                        
                                                        var dir = Folder.selectDialog ("Choose a folder containing .ai files.");
                                                        var files = dir.getFiles("*.ai");
                                                        
                                                        function FindAndReplaceScript_AllOpenDocuments(aDoc){      
                                                            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;      
                                                                        }      
                                                                    }      
                                                                }  
                                                            };    
                                                            
                                                        for (var f = 0; f < files.length; f++) {
                                                                var doc = app.open(files[f]);
                                                            FindAndReplaceScript_AllOpenDocuments(doc); 
                                                            doc.close (SaveOptions.SAVECHANGES);
                                                            };
                                                        

                                                         

                                                        To push this one more step ahead:

                                                        We start with a dialogue where you can put in the text to search for.

                                                        Then comes a dialogue to put in the replacement.

                                                        After that you navigate to the folder with .ai files,

                                                        ... and lean back.

                                                         

                                                        No more need to edit the script so far.

                                                        2 people found this helpful
                                                        • 26. Re: Find and Replace Script for multiple files
                                                          Larry G. Schneider Adobe Community Professional & MVP

                                                          You've saved the file as an Rich Text File instead of a Plain Text File. Resave as a plain text .jsx file and retry.

                                                          4 people found this helpful
                                                          • 27. Re: Find and Replace Script for multiple files
                                                            penkat83858457 Level 1

                                                            Hey Guys, thank you very, very much for your help - It all works very well now, you saved me a lot of time!