Skip to main content
Participant
May 21, 2026
Question

Illustrator and scripting inconsistent words[] when tabs used

  • May 21, 2026
  • 1 reply
  • 20 views

Can someone explain the results when the following is run on an Illustrator document with one text frame?

var myDoc = app.activeDocument;
var myTextFrame = myDoc.textFrames[0]
myTextFrame.contents = "Lorem ipsum dolor sit amet\rLorem\tipsum\tdolor\tsit\tamet";

alert(myTextFrame.words.length);
alert(myTextFrame.lines[0].words.length);
alert(myTextFrame.lines[1].words.length);

for (l=0; l<2; l++)
{
concat = "#";
for (i=0; i<myTextFrame.lines[l].words.length; i++)
{
concat += myTextFrame.lines[l].words[i].contents;
concat += "#";
}
alert(concat);
}

14

5

9

#Lorem#ipsum#dolor#sit#amet#

#Lorem#Lorem#ipsum#ipsum#dolor#dolor#sit#sit#amet#

Why does separating the words by tabs seem to duplicate all but the last word in the line?

Is this a bug, and if not, what is the logic behind it?

I believe I have the latest version of Illustrator downloaded via CC

    1 reply

    jduncan
    Community Expert
    Community Expert
    May 22, 2026

    Tab seems to be considered a character/word when included with other characters (a-z, etc.).

    var myDoc = app.activeDocument;
    var myTextFrame = myDoc.textFrames[0];
    myTextFrame.contents = "a\t\t\t\tb";

    alert(myTextFrame.words.length);
    // result is 6

    Please note, when the textframe is just whitespace (like below), the tab characters are not counted as words...

    var myDoc = app.activeDocument;
    var myTextFrame = myDoc.textFrames[0];
    myTextFrame.contents = "\t\t\t\t";

    alert(myTextFrame.words.length);
    // result is 0

    Except if you separate with them spaces…

    var myDoc = app.activeDocument;
    var myTextFrame = myDoc.textFrames[0];
    myTextFrame.contents = "\t \t \t \t";

    alert(myTextFrame.words.length);
    // result is 3

    And I’m sure there are other weird edge cases. Unfortunately, the API docs don’t detail how AI handles whitespace so you would have to test for all edge cases if you needed this to function correctly in production.

     

    You could count the words yourself with a custom function instead of relying on the `.words` method.

    function countRealWords(tf) {
    var s = tf.contents || "";
    var matches = s.match(/[A-Za-z0-9]+(?:['’-][A-Za-z0-9]+)*/g);
    return matches ? matches.length : 0;
    }