Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Search for a certain letter in the last line of a text

  • Search for a certain letter in the last line of a text

    Posted by Niels Karner on July 13, 2023 at 3:15 pm

    I am trying to search for a certain letter in the last line of a text
    with always different numbers of lines. I have already managed the
    search for the letter, I am only missing the possibility to limit the
    search to the last line. Sometimes it is only one line and sometimes two
    or even more.

    var t = sourceRectAtTime().top;
    var h = sourceRectAtTime().height;
    var y = t + h;
    var l = y-5;
    var Q = y-3.5;
    var J = y-4;

    var txt = text.sourceText;

    var findlower = txt.includes(“q”)||txt.includes(“p”)||txt.includes(“g”)||txt.includes(“j”)||txt.includes(“y”);
    var findQ = txt.includes(“Q”);
    var findJ = txt.includes(“J”);

    if(findlower == true){[0,l]}
    else if(findJ == true){[0,J]}
    else if(findQ == true){[0,Q]}
    else [0,y];

    I tried it with .split(“r”)[x]; but I can only select a certain line, which doesn’t help if I don’t know which one is the last.

    Thanks for helping me!

    Brie Clayton replied 3 years ago 4 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    July 13, 2023 at 3:49 pm

    This should give you the last line:

    txt = text.sourceText;
    lines = txt.split("\r");
    lastLine = lines[lines.length-1];
  • Filip Vandueren

    July 15, 2023 at 6:20 pm

    It looks like you are testing for the case that a letter with an ascender will change the sourceRect.

    A good alternative is to change all characters to a character that has full height for your font (using a text animator) and check the sourceRect.

    You can do that on a second hidden text-layer, or there are tricks to do it on the same layer: in negative time, inbetween frames so you would do sourceRectAtTime(-1) and have a text animator with change character that’s only active if time<0

  • Niels Karner

    July 19, 2023 at 12:39 pm

    That sounds very interesting, can you give me an example of that?😄

  • Filip Vandueren

    July 19, 2023 at 8:00 pm

    Sure, suppose you want to have a sourceRect for a textlayer, but you want that sourcerect to be tall enough to contain descenders, even if there aren’t any in the current text. (useful if you want to line up multiple

    Choose a character for your current font that has a full height both above and below the baseline, for example the pipe symbol “|” is good in many fonts. (Or you can use a character that doesn’t have any ascenders or descenders like “x” , but then add a margin you know will be enough for the font)

    Add a “character value” textanimator to your text, with this expression:

    "|".codePointAt(0);

    You’ll see all the characters change to |. If you would take the top and height of the sourceRect of the layer now, you’ll get the margin of the descenders.

    But if you change the expression to:

    time < 0 ? "|".codePointAt(0) : 0;

    Everything is normal again, the text will only be changed to |||| when the time <0.

    So now you can do this:

    sr1 = sourceRectAtTime(time);
    sr2 = sourceRectAtTime(-1);
    l = sr1.left;
    w = sr1.width;
    t = sr2.top;
    h = sr2.height;

    The horizontal parameters are based on the actual text, the vertical parameters are based on the text as it exists (hidden) in negative time: every character replaced by “|”

    You may want to add an extra margin because stuff like Á is taller than |

    I hope that’s clear.

  • Niels Karner

    July 20, 2023 at 6:19 am

    This works perfectly and is by far better than what I was doing before. Thanks a lot for this!

    @Dan Ebberts your solution has also helped me a lot. Thank you too!

  • Brie Clayton

    July 20, 2023 at 2:15 pm

    Thanks for your solve, Filip!

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