Skip to main content
Participant
April 10, 2025
Answered

Scripting: Stop function from running on all open documents

  • April 10, 2025
  • 1 reply
  • 303 views

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

 

Correct answer RobOctopus

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

1 reply

RobOctopus
RobOctopusCorrect answer
Inspiring
April 10, 2025

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();
Deb5C67Author
Participant
April 10, 2025

That did the trick, thank you very much!

Community Expert
April 11, 2025

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

-Manan