• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

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(); 
TOPICS
Scripting

Views

4.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Contributor ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Hello.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 07, 2017 Dec 07, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 12, 2017 Dec 12, 2017

Copy link to clipboard

Copied

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);
    FindAndReplaceScript_OpenDocuments(docRef);
    docRef.saveAs (fileObjectList);
    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;
                var newString = aTF.contents.replace(searchString, replaceString);
                if (newString != aTF.contents) {
                   theTF.contents = newString;
                }
           }
        }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2017 Dec 12, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

you can reply to me at franklin.smart@rhein.cl, the error that show is 1242 at line 27

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

Hi @Disposition_Dev thanks for posting that script here. I'm trying to replace periods for commas across multiple Illustrator documents. I want to use your script to do so but when I run it I get the attached error message. If it helps I'm running it on the 2022 version of Illustrator.Screen Shot 2021-11-18 at 12.30.11 PM.png
Any help much appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

LATEST

Hey friend. When the forums were migrated to the new format a while back, the indexes got removed from all the code that was written in. I've edited that reply to have the appropriate indeces. But i also rewrote that script into a cleaner and more reliable version which you can find below: 

function container()
{

	//enter find and replace terms here
	var searchString = /VT/gim; 
	var replaceString = " VHB ";


	// 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$/;


	var aDoc;
	var theTF;
	var aTF;
	var newString;

	

	// Opens each file in the given folder
	for (var i = 0; i < fileObjectList.length; i++)
	{
		if (filePat.test(fileObjectList.name))
		{
			continue;
		}

		app.open(fileObjectList[i]);

	}

	//activate each open document in turn and find replace 
	function FindAndReplaceScript_AllOpenDocuments()
	{

		for (var i = app.documents.length - 1; i > -1; i--)
		{
			app.documents.activate();
			aDoc = app.documents;
			theTF = aDoc.textFrames;

			

			for (var j = 0; j < theTF.length; j++)
			{
				aTF = theTF[j];
				newString = aTF.contents.replace(searchString, replaceString);

				if (newString !== aTF.contents)
				{
					theTF.contents = newString;
				}
			}

		}

	};

	FindAndReplaceScript_AllOpenDocuments();
}
container();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines