I'm trying to write an expression that resizes a shape layer depending on the size of a paragraph text layer.
I could just use sourceRectAtTime() *but* if there's only one line, I want to size the box to the width of the text. If it runs over two or more lines, the box should be a fixed (maximum) width. With non-paragraph text you can just count the number of carriage returns -
L = thisComp.layer("text_layer");
tx = L.text.sourceText.valueAtTime(time);
h = tx.split('\r');
n = h.length; // how many lines we've got
But I can't think of a way to interrogate the paragraph text layer to find out how many lines it's running to. Is there one, or will this be a case of manually keyframing?
But since you are interested in sourceRect itself, you may duplicate your text layer and apply Animator for Character Value = 1 with Expression Selector textIndex<=4 ? 0 : 100 based on lines.
What it does is it replaces all characters in lines after [for ex.] 4th with #1 Unicode character which is not displayed in AE (in your system too, hopefully). Effectively it means that only first 4 lines are displayed with sourceRect adjusted accordingly.