# Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions finding the longest text

• # finding the longest text

Posted by on May 10, 2022 at 5:59 pm

Hi all, if i have an if else statement in a project that controls the length of a solid based on the length of the text.

It was easy for 2 text layers, because i assigned x to the length of a text layer with sourcerectattime, and y to the other text layer. and I am using a specific time also because of the animation of the text ( so lets say the 2 second mark )

Now I have more layers, and also some of these layers are coming in later, lets say 7 seconds. So I am not sure how to solve this without making this more complicated.

here is my original expression,

gap = 36;

L = thisComp.layer(“TEXT1”);

M = thisComp.layer(“TEXT2”);

rect = L.sourceRectAtTime();

rect2 = M.sourceRectAtTime();

x = L.toComp([rect.left+rect.width,0],2)[0];

y = M.toComp([rect2.left+rect2.width,0],2)[0];

xclamp=clamp(x,545.70,2000);

yclamp=clamp(y,545.70,2000);

if (x>y) xclamp+gap;

else yclamp+gap

now if I only add 1 more layer, my expression would be this

gap = 36;

L = thisComp.layer(“TEXT1”);

M = thisComp.layer(“TEXT2”);

N = thisComp.layer(“TEXT3”);

rect = L.sourceRectAtTime();

rect2 = M.sourceRectAtTime();

rect3 = N.sourceRectAtTime();

x = L.toComp([rect.left+rect.width,0],2)[0];

y = M.toComp([rect2.left+rect2.width,0],2)[0];

z = N.toComp([rect3.left+rect3.width,0],7)[0];

xclamp=clamp(x,545.70,2000);

yclamp=clamp(y,545.70,2000);

zclamp=clamp(z,545.70,2000);

if (x>y)&&(x>z) xclamp+gap;

if (y>x)&&(y>z) yclamp+gap;

else zclamp+gap

so if i had 6 text layers, this expression would be extremely long. Is there a function I am not aware of that would allow me to bypass writing 5 if else statements each having 5 ”&&” like this for example ;

( this is just 1 line )

if (x>y)&&(x>z)&&(x>z2)&&(x>z3)&&(x>z4) xclamp+gap; and so on….

thanks for any help

2 Members · 6 Replies
• 6 Replies
• ### Dan Ebberts

May 10, 2022 at 6:38 pm

I think I’d do it with a loop. This won’t be exactly right (I haven’t tested it at all), but maybe it’ll get you headed in the right direction:

`gap = 36;layers = ["TEXT1","TEXT2","TEXT3"];times =  [    2,          2,         7  ];maxX = 0;for (i = 0; i < layers.length; i++){  L = thisComp.layer(i);  rect = L.sourceRectAtTime();  x = L.toComp([rect.left+rect.width,0],times[i])[0];  xclamp=clamp(x,545.70,2000);  maxX = Math.max(maxX,xclamp);}maxX+gap`

May 10, 2022 at 6:58 pm

thanks so much. I will test it and come back and post with the outcome

May 10, 2022 at 7:07 pm

Im getting an error here;

L = thisComp.layer(i);

layer index 0 out of range

May 10, 2022 at 7:44 pm

t1 = thisComp.layer(“TEXT 1”).toComp([thisComp.layer(“TEXT 1”).sourceRectAtTime().left+thisComp.layer(“TEXT 1”).sourceRectAtTime().width,0],2)[0];

t2 = thisComp.layer(“TEXT 2”).toComp([thisComp.layer(“TEXT 2”).sourceRectAtTime().left+thisComp.layer(“TEXT 2”).sourceRectAtTime().width,0],2)[0];

t3 = thisComp.layer(“TEXT 3”).toComp([thisComp.layer(“TEXT 3”).sourceRectAtTime().left+thisComp.layer(“TEXT 3”).sourceRectAtTime().width,0],2)[0];

t4 = thisComp.layer(“TEXT 4”).toComp([thisComp.layer(“TEXT 4”).sourceRectAtTime().left+thisComp.layer(“TEXT 4”).sourceRectAtTime().width,0],2)[0];

t5 = thisComp.layer(“TEXT 5”).toComp([thisComp.layer(“TEXT 5”).sourceRectAtTime().left+thisComp.layer(“TEXT 5”).sourceRectAtTime().width,0],2)[0];

t6 = thisComp.layer(“TEXT 6”).toComp([thisComp.layer(“TEXT 6”).sourceRectAtTime().left+thisComp.layer(“TEXT 6”).sourceRectAtTime().width,0],2)[0];

xclamp = clamp(Math.max(t1,t2,t3,t4,t5,t6)+20,545.70,2000);

[xclamp,value[1]]

• ### Dan Ebberts

May 10, 2022 at 8:42 pm

Yes, sorry–that should have been:

`L = thisComp.layer(layers[i]);`