2 Replies Latest reply on Mar 11, 2017 1:09 AM by nathanwells

    Array Find and Replace Script?

    nathanwells Level 1

      I've found a script that will find and replace a term in an Illustrator file perfectly, but I was wondering if someone would be willing to modify the script to allow me to find and replace multiple items in an array?

       

      I've looked around quite and bit and can't seem to find a script that would give me some clues on how to do this (my programming skills are limited).

       

      Here's the code for one term:

       

      var active_doc = app.activeDocument; 

       

      var search_string = /first/gi; // g for global search, remove i to make a case sensitive search 

      var replace_string = "ដាម៉ាស"; 

       

      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 so much!

        • 1. Re: Array Find and Replace Script?
          nathanwells Level 1

          This works:

          var active_doc = app.activeDocument; 

           

           

           

           

          var replaceChars={ "Damascus ":"ដាម៉ាស" , "Rabbah ":"រ៉ាបាត" };

           

           

          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(/Damascus |Rabbah /g,function(match) {return replaceChars[match];})

                      

                     if (new_string != this_text_frame.contents) 

                         { 

                              this_text_frame.contents = new_string; 

                         } 

                } 

           

           

           

          But is there a way to list the replacements only once?

          • 2. Re: Array Find and Replace Script?
            nathanwells Level 1

            I was able to work it out with some help from those on another post: Find and Replace Script for multiple files
            Thanks to Silly-V

             

             

             

             

            function FindAndReplaceScript_AllOpenDocuments(){      
            
            
                for(var i=app.documents.length -1; i > -1;  i--){      
                    app.documents[i].activate();  
                    var aDoc = app.documents[i];  
            
            //List of words to replace
                    var mapObj = {
                        Damascus:"ដាម៉ាស",
                        Rabbah:"រ៉ាបាត",
                        Samaria: "សាម៉ារី"
                        };        
                          
                    var theTF = aDoc.textFrames;      
                    if (theTF.length > 0) {      
                        for (var j = 0 ; j <theTF.length; j++) {      
                            var aTF = theTF[j];     
                            for (var re in mapObj) {
                            var newString = aTF.contents.replace(re, mapObj[re])
                            if (newString != aTF.contents) {      
                                theTF[j].contents = newString;      
                            }      
                        }   
              }
                    }  
                }  
            };      
            FindAndReplaceScript_AllOpenDocuments();