Skip to main content
Known Participant
April 26, 2021
Question

Using Find/Replace as part of an action

  • April 26, 2021
  • 4 replies
  • 5479 views

Hi folks,

 

I'm trying to create an action which will replace the text in a box containing the phrase '??????'.

 

When I create the action using the menu item and run it, it works perfectly. However, when I restart Illustrator the action appears to skip the find/replace step entirely.

 

Any ideas on how to address this?

 

Thanks in advance!

This topic has been closed for replies.

4 replies

pixxxelschubser
Community Expert
Community Expert
April 26, 2021

@mikes82936200 

this code from the first post sounds strangely familiar to me - even though it's garbled.

At that time, I rarely left credits in my codes

😉

 

base code (my code wrapped into a function by @Silly-V )

Find and Replace Script for multiple files

 

more complex with same base code:

Multiple Line Replace Script? 

 

Don't worry, I think it's a good thing you tried to mention the credits. And I hope Williams supplement helps you further.

Known Participant
April 27, 2021

D'oh, sorry @pixxxelschubser!! Belated thanks for your work, helped me so much yesterday and I'll check out the complex code later today.

 

Thanks again!

Known Participant
April 26, 2021

Thanks for the replies! I found a JS code which I'm using as part of the action instead, and which appears to be working great. Code is below (apologies for not crediting the author, the page seems to have vanished from my history!):

 

function FindAndReplaceScript_AllOpenDocuments(){      
        for(var i=app.documents.length -1; i > -1;  i--){      
        app.documents[i].activate();  
        var aDoc = app.documents[i];  

        var searchString = /OLDTEXT/gi;     
        var replaceString = 'NEWTEXT';       

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

 

Just replace 'OLDTEXT' with 'NEWTEXT'. Worth pointing out, this carries out the script on ALL open documents, although I'm sure someone more experienced with JS can modify it 🙂

Disposition_Dev
Community Expert
Community Expert
April 26, 2021

here you go. This one will do all open documents, or just the active document. You can choose which one you want to use by toggling the comments on lines 4 and 8.

 

function FindAndReplaceScript()
{
	//to process all open documents
	var filesToProcess = app.documents;

	//to process only active document
	//don't neglect the brackets. this needs to be an array/collection
	// var filesToProcess = [app.activeDocument];


	for (var i = filesToProcess.length - 1; i > -1; i--)
	{
		filesToProcess[i].activate();
		var aDoc = filesToProcess[i];

		var searchString = /OLDTEXT/gi;
		var replaceString = 'NEWTEXT';

		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();
Kurt Gold
Community Expert
Community Expert
April 26, 2021

Unfortunately, this is a bug that has been there for decades.

 

Mike_Gondek10189183
Community Expert
Community Expert
April 26, 2021

Try setting your playback options from actions flyout menu to pause for 1 second, does that help?