Copy link to clipboard
Copied
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.
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 (the
Copy link to clipboard
Copied
Hi Silly-V, thanks, it works fine Btw, if I try to replace two words instead of one ( eg: intead of '001E' I write 'hello world'), it returns me syntax error on that line. Why?
Copy link to clipboard
Copied
It works fine for me, can you paste your line where you put in the "hello world" ?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hey guys, I've tried it, but it gives me a syntax error.
Do you have any idea why? Thanks!
Copy link to clipboard
Copied
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 🙂
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hey, thanks for your input. This sounds like a great idea, to save and close. I guess, I'm not doing something right, because it gives me the same error message - I'm also using MAC - tried on CS6 as well. ;/
Copy link to clipboard
Copied
You've saved the file as an Rich Text File instead of a Plain Text File. Resave as a plain text .jsx file and retry.
Copy link to clipboard
Copied
Are you running the script from ESTK or from AI?
Maybe we should add this as first line:
#target Illustrator
Copy link to clipboard
Copied
#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.
Copy link to clipboard
Copied
Hey Guys, thank you very, very much for your help - It all works very well now, you saved me a lot of time!
Copy link to clipboard
Copied
So I tried putting together all the scripts from this forum to create one single script that will:
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();