Skip to main content
Known Participant
November 21, 2021
Answered

Check if the selected layer contains a rectangle Extendscript

  • November 21, 2021
  • 1 reply
  • 966 views

Hey,

I can't find how to check if a layer has a certain proprety from this list

In my case, I want to check if the selected layer contains a rectangle, here's my code:

thisComp = app.project.activeItem;
curLayer = thisComp.selectedLayers[0];

iconbutton1.onClick = function() {
    if (curLayer instanceof ShapeLayer) { //check if it's a shape layer

        if () { // check if it contains a rectangle
            alert("this layer has rectangle");   
        } else {
            alert("this layer doesn't has rectangle");
        }

    } else {
        alert("Please select a shape layer");
    }
}

Thanks in advance,
RedyMotion

This topic has been closed for replies.
Correct answer DuncanL

There are probably a lot of ways you could accomplish this, and it could get fairly complex to do a comprehensive check, but here I just for looped through the "Contents" property of the layer to see if it contains a property called "Rectangle 1" which is the default name when a new rectangle layer is created.

 

var thisComp = app.project.activeItem;
var curLayer = thisComp.selectedLayers[0];
var rectProp = false;

iconbutton1.onClick = function() {
    if (curLayer instanceof ShapeLayer) {
            for (i = 1; i <= curLayer.numProperties; i++) {
                if (curLayer.property("Contents").property(i).name == "Rectangle 1") {
                    alert("This layer does have a rectangle!");
                    rectProp = true;
                }
            }
            if (rectProp == false) {
            alert("This layer doesn't have a rectangle.");
            }
        }
    }

1 reply

Mylenium
Legend
November 21, 2021

You simply iterate through the properties and check against the type. Not sure what else you need to know here.

 

Mylenium

Known Participant
November 21, 2021

Thanks!

How do I do that tho ( like a line of code )? I tried with multiple ways but none worked.

RedyMotion

DuncanL
DuncanLCorrect answer
Inspiring
November 21, 2021

There are probably a lot of ways you could accomplish this, and it could get fairly complex to do a comprehensive check, but here I just for looped through the "Contents" property of the layer to see if it contains a property called "Rectangle 1" which is the default name when a new rectangle layer is created.

 

var thisComp = app.project.activeItem;
var curLayer = thisComp.selectedLayers[0];
var rectProp = false;

iconbutton1.onClick = function() {
    if (curLayer instanceof ShapeLayer) {
            for (i = 1; i <= curLayer.numProperties; i++) {
                if (curLayer.property("Contents").property(i).name == "Rectangle 1") {
                    alert("This layer does have a rectangle!");
                    rectProp = true;
                }
            }
            if (rectProp == false) {
            alert("This layer doesn't have a rectangle.");
            }
        }
    }