Copy link to clipboard
Copied
I have this script that will do a word replace on all text in a document (changes all words to a single word you type in). Problem is, this script will run on all of my open documents instead of the currently active document. Could I get some help editing this please? I've tried idoc = app.Activedocument but it breaks it. Thanks.
#target Illustrator
function Fontcheck() {
var idoc, tframes, i, tframe, new_string, counter = 0;
var replace_string = prompt("Word","");
for (var a = 0; a < app.documents.length; a++) {
idoc = app.documents;
tframes = idoc[a].textFrames;
for (i = 0; i < tframes.length; i++) {
tframe = tframes[i];
new_string = tframe.contents.replace(tframe.contents, replace_string);
if (new_string != tframe.contents) {
tframe.contents = new_string;
counter++;
}
}
}
};
Fontcheck();
the for loop for the documents is running for however many documents there are
for (var a = 0; a < app.documents.length; a++)
remove that for loop and the brackets it uses
function Fontcheck() {
var tframes, i, tframe, new_string, counter = 0;
var replace_string = prompt("Word","");
/*removed for loop and got rid of iDoc variable. seemed pointless*/
tframes = app.activeDocument.textFrames;
for (i = 0; i < tframes.length; i++) {
tframe = tframes[i];
...
Copy link to clipboard
Copied
the for loop for the documents is running for however many documents there are
for (var a = 0; a < app.documents.length; a++)
remove that for loop and the brackets it uses
function Fontcheck() {
var tframes, i, tframe, new_string, counter = 0;
var replace_string = prompt("Word","");
/*removed for loop and got rid of iDoc variable. seemed pointless*/
tframes = app.activeDocument.textFrames;
for (i = 0; i < tframes.length; i++) {
tframe = tframes[i];
new_string = tframe.contents.replace(tframe.contents, replace_string);
if (new_string != tframe.contents) {
tframe.contents = new_string;
counter++;
}
}
};
Fontcheck();
Copy link to clipboard
Copied
That did the trick, thank you very much!
Copy link to clipboard
Copied
Hi @Deb5C67 ,
Isn't the use of replace method irrelevant here. As I see new_string would always be equal to replace_string as you are replacing the whole content of the frame with replace_string. So we can just get rid of the replace method and directly set the contents of the frame. Something like the following
function Fontcheck() {
var tframes, i, tframe, counter = 0;
var replace_string = prompt("Word","");
tframes = app.activeDocument.textFrames;
for (i = 0; i < tframes.length; i++) {
tframe = tframes[i];
if (tframe.contents != replace_string) { // Check if content needs changing
tframe.contents = replace_string;
counter++;
}
}
};
-Manan