How to use Source Text property with Scripting?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
BigMacGyver
Posts: 19
Joined: May 7th, 2013, 6:43 am

Hey guys,

i have a quick question for you that is currently slowing me down on a scripting project.

When i use the following code to alert the length of a source text string from the first layer within a comp, i just get an error:

Code: Select all

 app.beginUndoGroup("get text length");

			var comp = app.project.activeItem; //selected composition

  txt = comp.layer(1).text.sourceText.value.text.length(); // get selected comp's first text layer's text length (amount of characters)
  
  alert(txt);

app.endUndoGroup();
The error says "function comp.layer(1).text.sourceText.value.text.length();" is undefined. I have tried different variations of this line but i never get a sufficient result. The only variant that actually returns something is "comp.layer(1).text.sourceText.value;" which alerts the text written in the source text property.

But i need to do various length checks to compare line lengths, amount of line breaks etc. .split() is not working either. What am I doing wrong? In expressions, this was never a problem. I must be missing just a tiny detail to make it work.
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

I think you just need to remove the parentheses from length(). It's being interpreted as a function rather than a attribute.

Dan
BigMacGyver
Posts: 19
Joined: May 7th, 2013, 6:43 am

Thanks, dan. It's always the little things.

Now i have tried to evolve this a little to get the length of multiple text layers within a specified index range. It does not work though.

Code: Select all

var comp = app.project.activeItem;
var startIdx = 1;
var stopIdx = comp.numLayers- 8;

for (idx = startIdx; idx <= stopIdx; idx++){
var newText = comp.layer[idx].text.sourceText.value.text.split("\r");
}

I am trying to first get the longest line with most chars from each of the layers within the selected range and then compare these largest lines with each other to find the largest line of all layers (if that makes sense). I am trying to get the largest extend of the text.

Or is there a better way to find the largest extend of text layers by using scripts? The above worked fine with expressions but ultimately is not very precise for certain fonts and i am having trouble to turn the expression i used into a script.
BigMacGyver
Posts: 19
Joined: May 7th, 2013, 6:43 am

Sorry, i am going to answer my own question.

Online i stumbled over the method sourceRectAtTime(), which is a blast!

The following code will alert the largest horizontal extend in pixels of a number of text layers within a specified index range:

Code: Select all

var comp = app.project.activeItem;

var creditLayers = comp.selectedLayers;

var startIdx = 1;
var stopIdx = comp.numLayers- 8;
var TextWidthData = new Array();

for (idx = startIdx; idx <= stopIdx; idx++){
    TextWidthData.push(comp.layer(idx).sourceRectAtTime(0, true).width);
    }
TextWidthData.sort();
TextWidthData.reverse();
alert(TextWidthData[0]);
It can be so nice and simple!
Post Reply