Forum Replies Created

Page 1 of 2
  • Dan, thank you very much this is brilliant! I think this will do exactly what I am looking for, thank you again for your help!

  • Hey Dan,

    That looks great, I think that will do exactly what I’m looking for. However I’m having an issue with inserting quote characters, I’m starting my table at 32 to allow spaces and some common symbols to be used.
    However, when I get to spots 34 or 39 (either of the quote characters) the script breaks, is there a way I can selectively escape these out?

  • Hey Dan,

    I know what I’m trying to do is probably a bit unusual, thanks for bearing with me on this one.

    I’m trying to setup an informational lower third that an editor can copy past some text into and it will auto resize itself based on what is entered. The lower third will also look at the text and add line breaks when it gets to long to help keep everything in the box. I have some code to add the line breaks, however it is inserting them at specific character positions in the string. Because the string may sometimes contain wider characters (the font is variable width) and the box size will be changing based on the string, I’d rather use the string pixel width to denote where in the main string a line break should be entered.

    What I’m hoping to do is setup a trigger that looks for when the string pixel length crosses a certain threshold, then once it does, it grabs that strings current length and use it as the position to insert the line break.

    While I have been able to grab the current string length once the trigger is crossed, it continues to update as the string grows, I’m trying to find someway to have After Effects look for when the threshold occurs, grab that value, retain it and then stop changing it.

    I know I could tie the break point to an expression slider to have it be easily change able but the text will be entered by the editor inside Premiere Pro (Using 2014’s ability to share AE text with Premiere) so the editor won’t have access to the AE sliders.

    Hopefully this helps explain what I’m trying to do a bit better, though so far I haven’t been able to figure out how to retain the string length. Any thoughts on how I might accomplish this?

    Thanks Dan!

  • Hmmm, okay. So, while the value technically doesn’t change over time it does change depending on what is entered by the user. I’m trying to find some way to catch the .length value of a string at the point the entered string became too long and store that value. The problem I’m having is the variable constantly updates as the string length grows. I tried placing it in a function which is returning the value, then only calling it while true. My though process being that since the function isn’t being called again it’s last value would still be remembered, but that doesn’t appear to be the case. Any thoughts on what I could try in this case, where time isn’t a factor?

    //define the variables
    var str = thisComp.layer(“Text Input”).text.sourceText;
    var pxCounter = thisComp.layer(“pxCounter”).text.sourceText;
    var pxl;

    //define counterTrigger function
    function counterTrigger (lng) {
    lng = str.length;
    return lng;
    }

    // if statement to run function while string length is less than 2000 pixels, otherwise show last
    // length

    if (pxCounter<2000) {
    pxl = counterTrigger (pxl);
    } else {
    pxl;
    }

  • Hey Dan,

    Great, thanks for the suggestion, I’ll look into this! Will this still work if the value of pxCounter is going to always be the same at all frames and doesn’t change? The pxCounter Width is based on a user input string, while that string my vary depending on what is entered, once its entered it will remain constant throughout the comp.

    Can I use .valueAtTime() and just pick a frame to have it store that value for me? Say:

    var pxCounter = thisComp.layer("var CharCounter4OverlayLength").text.sourceText.valueAtTime(1);

  • Hey Dan,

    I’m trying to create a bit of code that will give me the length of a string (in characters) right up until it’s pixel length crosses a certain threshold, or if it has already crossed that threshold how many characters are in the string up to that point.

    I’m trying something like this, but its just returning undefined once the value is greater than 2000.

    //define the variables
    var str = thisComp.layer("Text Input").text.sourceText;
    var pxCounter = thisComp.layer("pxCounter").text.sourceText;
    var pxl;

    //define counterTrigger function
    function counterTrigger (lng) {
    lng = str.length;
    return lng;
    }

    // if statement to run function while string length is less than 2000 pixels, otherwise show last
    // length

    if (pxCounter&lt;2000) {
    pxl = counterTrigger (pxl);
    } else {
    pxl;
    }

  • Bryce Poole

    October 19, 2014 at 6:20 pm in reply to: Auto Text Wrap Using String Pixel Length

    Hey Dan,

    Thanks for the reply, that sounds good! I’m still rather new to JavaScript, so I’m not quite sure how to set that up. Could you give me any pointers? Also, I’m really stuck with how to modify my text wrap script to use this information once I have it.

    I keep running into the issue that I need to use .length to find the correct insert point, but when I pass a pixel width of say 2000 into the function as the ‘width’ variable the script breaks since its expecting character length.

    So far I’m trying this, its not returning any errors, but its also not working. I know I’m definitely making a mistake in there somewhere…

    //define variables
    var str = thisComp.layer(“ENTER TEXT”).text.sourceText; //user input, source of string
    var strPixLeng = parseInt(thisComp.layer(“var CharCounter4OverlayLength”).text.sourceText); //layer countering pixel width

    //call function
    var result = stringDivider(str, str.length, “n”, strPixLeng);
    result;

    // define function
    function stringDivider(str, width, spaceReplacer, strPixLeng) {
    if (strPixLeng>2000) {
    var p= width
    for (;p>0 && strPixLeng< 2000;p--) { } if (p>0) {
    var left = str.substring(0, p);
    var right = str.substring(p+1);
    return left + spaceReplacer + stringDivider(right, width, spaceReplacer);
    }
    }
    return str;
    }

    Any help on solving this would be appreciated.

    Thanks again!

  • Thank you Xavier! This solved it!

    Though, I’m not totally sure I understand what all the RegExpr parts are doing, if you have time would you mind breaking it down for me, just so I can fully wrap my brain around this?

    s*/g, “$1″+D).split(D); //specifically this bit

    In either case this totally solved it, thank you very much!

  • I think I may have just solved it by adding a simple if statement to the declaration of the strings array.
    Though there might still be a better way to do this? Thank you again for all the help!

    var textIn = thisComp.layer("Add Text Here").text.sourceText;
    var D = "#"; // or something else
    var strings = textIn.replace(/(\[(up|down|right|left)\])\s*(?!$)/g, "$1"+D).split(D); // an array of substrings of textIn

    if (textIn.match(/\[(up|down|left|right)\]/) ) {
    strings[1];
    }else{
    strings[0];
    }

  • Hey Xavier,

    Thank you for the response, I think this might work to solve my problem! This seems to work well as long as the first [direction] is present, but sometimes the string may not contain any directions at all, in which case I’d like to keep it whole. Would it be possible to add some type of null catch for when there isn’t a second string present? Ideally I’d like to set the string to a very long max value as a default, something like 9999.

    For example:
    “go [up] first” or “go [up] then [down]” both work but “go wherever” returns an error when calling the second index in the string.

    var textIn = thisComp.layer("Add Text Here").text.sourceText;
    var D = "#"; // or something else
    var strings = textIn.replace(/(\[(up|down|right|left)\])\s*(?!$)/g, "$1"+D).split(D); // an array of substrings of textIn
    strings[1];// returns error if string does not contain [up], [down], [left], or [right]

Page 1 of 2

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