Page 4 of 5

Posted: March 31st, 2007, 4:25 pm
by nab
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

Posted: March 31st, 2007, 4:48 pm
by vidpat
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?

Posted: April 3rd, 2007, 8:54 am
by bradshaw1965
Hey nab,

Once again, some nice sleuthing.

Dale

Posted: April 3rd, 2007, 8:56 am
by byronnash
How did you find that tidbit Dale?

Posted: April 3rd, 2007, 9:05 am
by bradshaw1965
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

Posted: April 3rd, 2007, 10:33 am
by byronnash
So, can you just plug in any menu item to that function and it will execute it?

Posted: April 3rd, 2007, 11:51 am
by bradshaw1965
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

Posted: April 5th, 2007, 9:39 am
by lloydalvarez
wow.. this is huge! way to go guys!! :D

wow!

Posted: May 17th, 2007, 4:41 pm
by austin
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.

Fit Text to Box

Posted: May 17th, 2007, 7:38 pm
by austin
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(); 

}

Posted: May 20th, 2007, 2:14 pm
by nab
Hi austin,
looks ok to me !

(right-click to open zoom options)
http://www.nabscripts.com/Forum/Scripts ... est000.swf

Posted: May 20th, 2007, 9:27 pm
by austin
wow. killer layout, nab. looks like a lot of fun can be had here :)

Posted: December 28th, 2007, 9:07 am
by byronnash
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?

Posted: December 28th, 2007, 1:28 pm
by nab
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.

Posted: December 29th, 2007, 7:33 pm
by byronnash
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.