Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Scripting: Stop function from running on all open documents

New Here ,
Apr 10, 2025 Apr 10, 2025

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

 

TOPICS
Scripting
76
Translate
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

correct answers 1 Correct answer

Participant , Apr 10, 2025 Apr 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];
     
...
Translate
Adobe
Participant ,
Apr 10, 2025 Apr 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();
Translate
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 ,
Apr 10, 2025 Apr 10, 2025

That did the trick, thank you very much!

Translate
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 ,
Apr 10, 2025 Apr 10, 2025
LATEST

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

Translate
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