Determining Width and Height of a Text Layer

Find out why the . goes before the /

Moderator: Paul Tuersley

nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Hi guys,

actually we don't need any plug-in to retrieve the text layer dimensions, scripting provides all we need. Less than ten lines of scripting are required.

the key is the use of the -undocumented- "executeCommand()" application method.

we can create a new mask (default rectangular mask) around the text and retrieve its dimensions with simple math on mask vertices coords (right-left, top-bottom)

we get the command id by calling the -undocumented- findMenuCommandId(), that takes the name of the command as argument.

here is a example that illustrates this technique:

Code: Select all

{
	app.executeCommand(2367); // 2367 : app.findMenuCommandId("New Mask")
	var myTextLayer = app.project.activeItem.selectedLayers[0];
	var myMask      = myTextLayer.Masks.property(myTextLayer.Masks.numProperties);
	var myVertices  = myMask.maskShape.value.vertices;
	var TextWidth   = Math.abs(myVertices[0][0] - myVertices[3][0]);
	var TextHeight  = Math.abs(myVertices[0][1] - myVertices[1][1]);
	alert("Size of text in pixels:\t\r\r" +
	      "   width\t = " + TextWidth.toFixed(3) + "\r" +
	      "   height\t = " + TextHeight.toFixed(3));
	myMask.remove();
}
http://www.nabscripts.com/Forum/Scripts ... nsions.jsx
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

Fascinating. I'll have something to look forward to whenever I get around to upgrading from 6.5.

Am I to presume this works equally well when the text in the layer extends beyond the bounds of the comp?
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

Hey nab,

Once again, some nice sleuthing.

Dale
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

How did you find that tidbit Dale?
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

Hey Byron,

Chris Prosser from Adobe posted app.executeCommand(app.findMenuCommandId(”Auto-Orient…”)) in reference to an auto-rig script I was having difficulty writing. nab was clever enough to figure out the mask from "New Mask" would be the same dimensions as the text layer.

Dale
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

So, can you just plug in any menu item to that function and it will execute it?
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

byronnash wrote:So, can you just plug in any menu item to that function and it will execute it?
pretty much. If the menu command throws up a GUI dialog the user will still have to deal with that.

Dale
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

wow.. this is huge! way to go guys!! :D
austin
Posts: 9
Joined: June 21st, 2005, 2:08 pm

this is really, REALLY, great. Thanks!

I've been having troubles with the app.executeCommand (esp in the cs3 beta) but I got around that by just leaving a mask there in the source project, and then removing it after i get the size of the text layer.
austin
Posts: 9
Joined: June 21st, 2005, 2:08 pm

After playing with this for a while, I realized you need to compensate for the anchor point when scaling. Whipped this up - let me know what you guys think:

Code: Select all

function fitTextToBox(myTextLayer,boxX,boxY){

	var myMask;
	var myVertices;
	var thePosition;
	var TextWidth;
	var TextHeight;
	var NewScale;


	myTextLayer.selected=true;
	app.executeCommand(2367); // 2367 : app.findMenuCommandId("New Mask") 
	myMask   = myTextLayer.Masks.property(myTextLayer.Masks.numProperties); 
	myVertices = myMask.maskShape.value.vertices; 
	
	//re-set the anchor point to account for 1st character margin
	//assumes left justification
	if(myVertices[0][0]!=0){
		theAnchorPoint=myTextLayer.property("Anchor Point").value;
		theAnchorPoint[0]=theAnchorPoint[0]+myVertices[0][0];
		theAnchorPoint[1]=theAnchorPoint[1]+myVertices[1][1];
		myTextLayer.property("Anchor Point").setValue(theAnchorPoint);
	}
	
	textX = Math.abs(myVertices[0][0] - myVertices[3][0]); 
	textY = Math.abs(myVertices[0][1] - myVertices[1][1]); 
	
	newX=(100*boxX)/textX;
	newY=(100*boxY)/textY;

	myTextLayer.property("Scale").setValue([newX,newY]);
	
	myMask.remove(); 

}
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Hi austin,
looks ok to me !

(right-click to open zoom options)
http://www.nabscripts.com/Forum/Scripts ... est000.swf
austin
Posts: 9
Joined: June 21st, 2005, 2:08 pm

wow. killer layout, nab. looks like a lot of fun can be had here :)
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

I'm having trouble getting this to work on a script that loops through a lot of layers/comps. It seems like the comp that the layer is in needs to be active but there is no way to set the activeItem. How are you guys handling this problem?
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Yes that's right. You could try to create new text layers in the activeItem based on the text source of layers that are located in other comps to evaluate their size.
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Wow, that's a tough one, I'm not sure what the activeItem is going to be each time. I guess I'll do some testing and see how it works.
Post Reply