Skip to main content
Participant
December 6, 2017
Question

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

  • December 6, 2017
  • 3 replies
  • 5987 views

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.activate(); 
  4.         var aDoc = app.documents
  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;     
  13.                 var newString = aTF.contents.replace(searchString, replaceString);     
  14.                 if (newString != aTF.contents) {     
  15.                     theTF.contents = newString;     
  16.                 }     
  17.             }     
  18.         } 
  19.     } 
  20. };     
  21. FindAndReplaceScript_AllOpenDocuments(); 
This topic has been closed for replies.

3 replies

Disposition_Dev
Legend
December 12, 2017

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.name))

    {

        continue;

    }

    app.open(fileObjectList[i]);

}function FindAndReplaceScript_AllOpenDocuments(){    

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

 

 

        app.documents.activate();

        var aDoc = app.documents;

            

        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.contents = newString;    

                }    

            }    

        }

    }

};    

FindAndReplaceScript_AllOpenDocuments();

franklin smart
Participant
September 16, 2019
Dear William, I tried your script but it doesn't work, could you help me, I need a batch to change multiples text on multiples illustrator files
franklin smart
Participant
September 16, 2019
you can reply to me at franklin.smart@rhein.cl, the error that show is 1242 at line 27
Participant
December 7, 2017

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

OMOTI
Inspiring
December 8, 2017

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);
}

Participant
December 11, 2017

Hi OMOTI, thanks for the help.

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

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

}function FindAndReplaceScript_AllOpenDocuments(){     

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

        app.documents.activate(); 

        var aDoc = app.documents

             

        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;     

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

                if (newString != aTF.contents) {     

                    theTF.contents = newString;     

                }     

            }     

        } 

    } 

};     

FindAndReplaceScript_AllOpenDocuments(); 

OMOTI
Inspiring
December 6, 2017

Hello.

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

Participant
December 6, 2017

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?

Disposition_Dev
Legend
December 6, 2017

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