set a new activeItem for {app.executeCommand()}

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
cdinic
Posts: 12
Joined: May 5th, 2008, 4:24 pm

I'm looping through a ton of layers and I want to run an app.executeCommand(2367); //newMask

it fails to make the new mask because I haven't set the current layer to be the active item. I've tried deselecting everything, then selected the layer I'm looping through but its not workin. :cry:

//get all layer and deselect them all
var mylayers = app.project.activeItem.layers;
if(mylayers){
//alert(mylayers.length);
for(k=1; k <= mylayers.length; k++){
mylayers[k].selected = false;
}
}
//Select target layer
compElement.layers.selected = true;
//if we have a selected layer, make a new mask
if(compElement.selectedLayers){
{app.executeCommand(2367);} // 2367 : app.findMenuCommandId("New Mask")
}


Ref: similar problem
http://forums.creativecow.net/readpost/2/926692

Thanks!
-C
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Although you dont need to use app.executeCommand to set a new mask for a layer. You add a mask by using the addProperty() method which is described on p 147 of the CS3 scripting guide:

app.project.item(index).layer(index).propertyGroupSpec.addProperty(name)

• When adding to an ADBE Mask Parade: “ADBE Mask Atom”, “Mask”.


-Lloyd
cdinic
Posts: 12
Joined: May 5th, 2008, 4:24 pm

I got the addProperty to make a new mask:

var myTextLayer = app.project.activeItem.selectedLayers[0];
myTextLayer.mask.addProperty("mask");

However, it creates a new mask with all points at the origin.

The app.executeCommand(2367); makes a new mask that is fit to the bounds of my text layer. This is important since I'm using the mask to calculate the size of the text object.

Moving on to the RamPreview trick...
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

In CS3 they added a fix for that:

AVLayer sourceRectAtTime() method

app.project.item(index).layer(index).sourceRectAtTime(timeT, extents)

Description
Retrieves the rectangle bounds of the layer at the specified time index, corrected for text or shape layer content.
Use, for example, to write text that is properly aligned to the baseline.
Parameters
timeT - The time index, in seconds. A floating-point value.
extents - True to include the extents, false otherwise. Extents apply to shape layers, increasing the size of
the layer bounds as necessary.
Returns
A JavaScript object with four attributes, [top, left, width, height].

-Lloyd
cdinic
Posts: 12
Joined: May 5th, 2008, 4:24 pm

RamPreview Trick worked great.

THANKS for your help.

here is what I've got

// Open the current CompItem using RamPreview hack
// remember the original work area duration
var duration = compElement.workAreaDuration;
compElement.workAreaDuration = 0.06;
compElement.ramPreviewTest("",1,"");
// Play nice and put things back the way they were
compElement.workAreaDuration = duration;
//Select the current layer to add New Mask
compElement.layers.selected = true;
//New Mask Command
{app.executeCommand(2367);} // 2367 : app.findMenuCommandId("New Mask")
var Y = 0; //Scale Percentage
var myTextLayer = compElement.layers
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]);
//myMask.remove();

//If the layer is too big, scale it down
var diff = (TextWidth - targetW);
if(diff>0){
Y = (100*(targetW/TextWidth));
myTextLayer.scale.setValue ([Y, Y]);
}else{//alert("Text is within bounds");}
compElement.layers.selected = false;//Unselect text layer
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

I would recommend you stay away from app.executeCommand and ramPreviewTest as both of those funcitons are unsupported and in my testing they are not very reliable. If all you want is to know the size of your text, you should really use sourceRectAtTime.. it would also require less code and would be much more efficient and most importantly it would work every time.

-Lloyd
cdinic
Posts: 12
Joined: May 5th, 2008, 4:24 pm

Yea. Saw that after I posted my app.command solution.

.sourceRectAtTime is working.

thanks
-C
cdinic
Posts: 12
Joined: May 5th, 2008, 4:24 pm

I just got schooled.

Here is the result. Nice clean, elegant text layer fitting
//Get Text Layer Width
var myTextLayer = compElement.layers
var rect = myTextLayer.sourceRectAtTime(1, true);
var TextWidth = rect.width;

//If the layer is too big, scale it down
if(TextWidth > targetW){
var Y = (100*(targetW/TextWidth));
myTextLayer.scale.setValue ([Y, Y]);
}
Thanks a million,
-Chris
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

cdinic wrote:I just got schooled.

Here is the result. Nice clean, elegant text layer fitting
//Get Text Layer Width
var myTextLayer = compElement.layers
var rect = myTextLayer.sourceRectAtTime(1, true);
var TextWidth = rect.width;

//If the layer is too big, scale it down
if(TextWidth > targetW){
var Y = (100*(targetW/TextWidth));
myTextLayer.scale.setValue ([Y, Y]);
}
Thanks a million,
-Chris

When I use this method to get the width of a text layer and then scale, would the width change or remain the same if I were to query the width again with sourceRectAtTime?

Here's the scenario:
I set the text value and then get the width from the sourceRectAtTime to place an image at the end of the text.

Here's the problem:
If the text line is longer than the max width I set, the script scales it down to the proper length but the placed image does not follow it. The image is still positioned based on the original width.

When I put an alert in to get the width before scaling and another alert in to get the width after the scale the width is the same. Not sure I understand why.
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

It's been a while since I've done it, but I would expect the scale to not affect the width from sourceRectAtTime(). This is because you're scaling the layer, but you're measuring the layer's source. You should be able apply the same scale factor to the width value to come up with the adjusted value.

Dan
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

Thanks Dan, I'll give that a try... Can't say I fully understand the whole rectAtTime thing... It's hard to visualize. I know that when I select the text layer the bounding box selection is around the text itself.

Would love to find a visual explanation of it.
Post Reply