Skip to main content
Inspiring
November 8, 2022
Answered

ExtendScript help for adding expression to shape layer properties

  • November 8, 2022
  • 1 reply
  • 1930 views

I'm brand new to ExtendScript, but know my way around expressions pretty well. I have a situation where I have mulitple AI vector layers in a comp that I want to convert to shape layers then add an expression to the stroke property of each. I've read some guides, posts, and the scripting reference, but am finding it hard getting any where.

 

The steps I would like to put in the script are:

  1.  Take the selected layer AI vector layer and create a shape layer from it (i.e. Create > Create paths from vector layer). I know I can use app.executeCommand(3973) for this.
  2.  Remove the " Outlines" text from the new layer name.
  3.  On the new shape layer, for all objects in all groups, add the following expression to the Stroke Width property:
       scaler = transform.scale[0];
       sWidth = thisComp.layer("control").effect("Stroke Width")("Slider").value;
       sWidth * (100/scaler);
     
    I'm hoping some smart scripters out there can get me started. 

 

Thanks!

This topic has been closed for replies.
Correct answer Dan Ebberts

I haven't tested this, but I'd try wrapping the stroke access in a try/catch and just skip that property (with continue) if there's an error:

myGroup = myContents.property(i);
try{
	myStrokeWidth = myGroup.property("ADBE Vectors Group").property("ADBE Vector Graphic - Stroke").property("ADBE Vector Stroke Width");
	myStrokeWidth.expression = myExpr;
}catch (err){
	continue;
}

1 reply

Dan Ebberts
Community Expert
Community Expert
November 8, 2022

This may not be exactly right for your situation but it should get you headed in the right direction:

function test(){
	var myExpr = 'scaler = transform.scale[0];\r' +
		'sWidth = thisComp.layer("control").effect("Stroke Width")("Slider").value;\r' +
		'sWidth * (100/scaler);';
	var myComp = app.project.activeItem;
	var myLayers = myComp.selectedLayers;
	if (myLayers.length == 0){
		alert("No layer selected.");
		return;
	}
	var myLayer = myLayers[0];
	var oldName = myLayer.name;
	var oldIndex = myLayer.index;
	app.executeCommand(3973);
	var newLayer = myComp.layer(oldIndex);
	newLayer.name = oldName;
	var myContents = newLayer.property("ADBE Root Vectors Group");
	var myGroup;
	var myStrokeWidth;
	for (var i = 1; i <= myContents.numProperties; i++){
		myGroup = myContents.property(i);
		myStrokeWidth = myGroup.property("ADBE Vectors Group").property("ADBE Vector Graphic - Stroke").property("ADBE Vector Stroke Width");
		myStrokeWidth.expression = myExpr;
	}
}
test();
AmozzaAuthor
Inspiring
February 3, 2023

I've been working with this script for a while and it's been running great. However, this week I've been receiving an error on some files: "Unable to execute script at line 29. Null is not an object."

 

After some investigation of the script and files, I see that some objects in the AI file are filled but do not have strokes. When the script converts this to the shape in AE, the AE shape layer group only contains a fill and no stroke. So the script gives an error and stops processing the rest of the groups.

 

I tried adding an if/then/else statemement to the for loop at the end as follows :

 

 

for (var i = 1; i <= myContents.numProperties; i++){
        if (myStrokeWidth == null) {i = i + 1} else { // If there is no stroke, move to next group;
        myGroup = myContents.property(i);
        myStrokeWidth = myGroup.property("ADBE Vectors Group").property("ADBE Vector Graphic - Stroke").property("ADBE Vector Stroke Width");
        myStrokeWidth.expression = myExpr;
        }
    }

 

However, this doesn't work and doesn't add the expression to any of the groups with strokes. What am I missing in this error checking?

 

Thanks

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
February 3, 2023

I haven't tested this, but I'd try wrapping the stroke access in a try/catch and just skip that property (with continue) if there's an error:

myGroup = myContents.property(i);
try{
	myStrokeWidth = myGroup.property("ADBE Vectors Group").property("ADBE Vector Graphic - Stroke").property("ADBE Vector Stroke Width");
	myStrokeWidth.expression = myExpr;
}catch (err){
	continue;
}