Skip to main content
Participant
November 10, 2015
Answered

Find and Replace Script for multiple files

  • November 10, 2015
  • 9 replies
  • 8621 views

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

           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.

This topic has been closed for replies.
Correct answer Silly-V

So this is what I have now:

function FindAndReplaceScript_AllOpenDocuments(){ 

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

          app.documents.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

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

        if (newString != aTF.contents) { 

            theTF.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.


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.activate();

        var aDoc = app.documents;

           

        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;   

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

                if (newString != aTF.contents) {   

                    theTF.contents = newString;   

                }   

            }   

        }

    }

};   

FindAndReplaceScript_AllOpenDocuments();

9 replies

Participating Frequently
April 11, 2019

So I tried putting together all the scripts from this forum to create one single script that will:

  1. Go through all my open documents
  2. Search and replace multiple queries based on my json objects

The problem with the script below is that it will only replace the text if the entire text frame matches the query. I need all or PART of the text to match the query. How would I do that?

#target Illustrator

function FindAndReplaceScript_AllOpenDocuments(){    

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

        app.documents.activate();

        var active_doc = app.documents;

var text_frames = active_doc.textFrames;

var allSearchReplaceStrings = [

{

  "search": "text1",

  "replace": "replace1"

},

{

  "search": "text2",

  "replace": "replace2"

},

];

var this_text_frame, thisSearchReplacePair;

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

    this_text_frame = text_frames;

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

          thisSearchReplacePair = allSearchReplaceStrings;

          if(this_text_frame.contents == thisSearchReplacePair.search){

              this_text_frame.contents = thisSearchReplacePair.replace;

          }

    }

}

    }

};    

FindAndReplaceScript_AllOpenDocuments();

Participant
May 18, 2018

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

Inspiring
May 17, 2018

Are you running the script from ESTK or from AI?

Maybe we should add this as first line:

#target Illustrator

Inspiring
May 17, 2018

#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;     

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

            if (newString != aTF.contents) {     

                theTF.contents = newString;     

                }     

            }     

        } 

    };   

   

for (var f = 0; f < files.length; f++) {

        var doc = app.open(files);

    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.

Participant
May 17, 2018

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

Do you have any idea why? Thanks!

Inspiring
May 17, 2018

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 :-)

Inspiring
May 17, 2018

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;

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

if (newString != aTF.contents) {

theTF.contents = newString;

}}}};   

for (var f = 0; f < files.length; f++) {

var doc = app.open(files);

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.

Participant
June 11, 2017

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.

ascotto
Known Participant
July 15, 2016

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!

Silly-V
Legend
July 15, 2016

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

ascotto
Known Participant
July 18, 2016

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.

pixxxelschubser
Community Expert
Community Expert
November 11, 2015

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;

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

        if (newString != aTF.contents) {

            theTF.contents = newString;

            }

        }

    }

Have fun

Participant
November 13, 2015

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

Silly-V
Legend
November 13, 2015

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

function FindAndReplaceScript_AllOpenDocuments(){

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

          app.documents.activate();

          // paste pixxxel's code starting here on this line

    };

};

FindAndReplaceScript_AllOpenDocuments();

Silly-V
Legend
November 10, 2015

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.

try67
Community Expert
Community Expert
November 10, 2015

[Moved to the Illustrator Scripting forum]