Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Capture number of lines in Paragraph Text for expression

  • Capture number of lines in Paragraph Text for expression

    Posted by Troy Poirier on January 24, 2019 at 9:07 pm

    I’m looking for a way to find the number of lines in a paragraph text box.

    The text is dynamically generated when I create a new composition or modify the composition name. It pulls the text from TSV data in a separate composition, and all the text flows/wraps in the paragraph text to create anywhere from 1 to 8 lines of text. I have a null object with a slider that I manually input the number of lines (needed for an animation unrelated to this question), but having this variable propagate automatically would save me time, as every week I need to update this slider within multiple compositions.

    I looked into the following options :

    1) Range Selector : obviously knows how many lines (“selector(“Range Selector 1”).end“) with “index” and “lines” set, but once set, the “end” number does not update with the new text.

    2) Expression Selector : knows how many lines and uses the variable “textTotal” to do the math, but I cannot figure out how to extract that variable for use elsewhere in the composition.

    Can’t seem to find an answer to this but I feel like it should be possible; and maybe I am way off and After Effects just doesn’t allow for this. Any help appreciated.

    Dan Ebberts replied 1 year, 3 months ago 8 Members · 10 Replies
  • 10 Replies
  • Alex Printz

    January 24, 2019 at 9:27 pm

    you could do something like measuring the height of your text box and dividing by the height of the leading between lines:

    var t = thePathToTheTextObject;
    var h = 12; //make this a size appropriate to the line height in pixels; can’t pull dynamically from character panel;
    var n = text.sourceRecAtTime(time)/h;

    I would suggest rigging h to a slider and test until it seems right, and then keep it there until it (might) break.

    Alex Printz
    Mograph Designer

  • Alex Printz

    January 24, 2019 at 9:28 pm

    whoops, that:

    var t = thePathToTheTextObject;
    var h = 12; //make this a size appropriate to the line height in pixels; can’t pull dynamically from character panel;
    var n = t.sourceRecAtTime(time)/h;

    Alex Printz
    Mograph Designer

  • Troy Poirier

    January 28, 2019 at 3:50 pm

    Thank you so much, Alex! I don’t know why your specific code didn’t work for me, but you set me off in the right direction with sourceRectAtTime(). I found a similar chunk of code from Dan Ebberts elsewhere that led me to the solution below. Only oddity in this code was that I had to add a “padding” variable in order for the position value to match the manual setting I had before. Not a big deal, and this saves me so much time!

    // text layer
    t = thisComp.layer("text");

    // paragraph text height
    r = t.sourceRectAtTime(time,true).height;

    // font line height + padding
    y = 75;
    p = y + 5;

    // paragraph height divided by line height = # of lines
    n = r/y;

    // removes any decimals
    n = n.toFixed(0);

    /* code below spits out the Y Position values
    for my 2 keyframes based on knowledge of
    how many lines, up to 8 lines */

    lines = 340 + p;
    c = 0;

    do {
    lines = lines - p;
    while (n > 1 && n < 9 && n != c);


    if (numKeys > 1){
    k1 = key(1).time;
    k2 = key(2).time;
    v1 = lines;
    v2 = 473;
    linear(time, k1, k2, v1, v2);
    } else {

  • John Millington

    October 23, 2019 at 6:41 pm

    Thanks for posting your code Troy ☺

    I was getting an expression error on the line with the while loop, but it looks like it’s because when the code is posted here, the less-than sign < is converted to a HTML entity & l t ; ( I’ve added spaces so it doesn’t change to the < symbol).

    Just replying in case it helps someone else.

  • Andrei Popa

    October 28, 2020 at 4:55 pm

    Just in case someone still needs this today.

    I needed to count the lines, stumbled on this solution and noticed some things were added by Adobe to after effects in the meantime.

    I tried this solution and it worked perfectly with any type of font/fontsize/leading.

    L = thisComp.layer("Text Layer");

    var newStyle =;

    var myLeading = newStyle.autoLeading ? newStyle.fontSize * 1.2 : newStyle.leading;

    H = L.sourceRectAtTime(time, false).height;

    rowNo = Math.ceil(H / myLeading);


    You may need to replace the first row with a link to your actual text layer.

  • Gerald mark Soto

    December 18, 2020 at 5:18 pm

    thank you so much. needed this. can I ask why the use of 1.2 in the expression. how does that help find the answer?

  • Andrei Popa

    January 6, 2021 at 2:21 am

    The autoleading value is 1.2. And sometimes when leading was different value but then set to autoleading, the expression took the value before the autoleading. So I have put that condition. If autoleading is true, than leading is fontsize*1.2

  • Kane Daniel

    April 22, 2022 at 5:38 am

    Thanks so much! This works perfectly. Thanks for posting.

  • Tim Parsons

    March 20, 2023 at 6:27 pm

    I’m getting error codes that say that

    “property or method named ‘style’ in Class ‘sourceText’ is missing or does not exist.” Same for autoLeading, fontSize, etc.

    In my text layer, I don’t see these variables, and have no idea how to tell AE to either show them or know where to look for them.

    Any ideas?

  • Dan Ebberts

    March 20, 2023 at 8:17 pm

    I’m guessing that you either have an old version of AE or you have File > Project Settings > Expressions > Expression Engine set to Legacy ExtendScript instead of JavaScript.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy