Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check if the selected layer contains a rectangle Extendscript

Explorer ,
Nov 20, 2021 Nov 20, 2021

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

TOPICS
Error or problem , How to , Scripting
767
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 21, 2021 Nov 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) {
      
...
Translate
LEGEND ,
Nov 21, 2021 Nov 21, 2021

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

 

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

Thanks!

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

RedyMotion

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 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.");
            }
        }
    }
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

Thank you so much! Exactly what I needed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

For some reason, this doesn't work...

If it finds a rectangle, it will alert: "This layer does have a rectangle!"
But if it doesn't find any rectangles, it doesn't alert anything even if I put an alert between the for loop and the " if (rectProp == false) " as if it wouldn't get out of the for loop...

 

thisComp = app.project.activeItem;
curLayer = thisComp.selectedLayers[0];
//curLayerIndex = thisComp.selectedLayers[0].index;
//curLayerAnchorPoint = [thisComp.width/2, thisComp.height/2]-curLayer.transform.position.value-curLayer.transform.anchorPoint.value
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.");
            }

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

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

Found the issue, the curLayer and rectProp variables either need to be initialized at the beginning of your onClick or you need to update it with an onChange.

 

Right now those variables are being set when the script is loaded, not updated when something changes. (i.e. thisComp.selectedLayers.onChange = function () { curLayer = thisComp.selectedLayers[0];}

 

This is working for me now:

 

var proj = app.project;
var thisComp = proj.activeItem;

iconbutton1.onClick = function() {
    var rectProp = false;
    var curLayer = thisComp.selectedLayers[0];
    contentsProp = null;
    if (curLayer instanceof ShapeLayer) {
        for (i = 1; i <= curLayer.numProperties; i++) {
            if (curLayer.property("Contents").property(x).name == "Rectangle 1") {
                alert("This layer does have a rectangle!");
                rectProp = true;
                break;
                }
            }
            if (rectProp == false) {
                alert("This layer doesn't have a rectangle.");
            }
        } else {
            alert("Please select a shape layer")
        }
    }

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

ignore "contentsProp", was messing around trying to find the issue. Doesn't actually do anything

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 21, 2021 Nov 21, 2021

Ohhh thank you so much! Also found another issue, the for loop should be :

 

for (i = 1; i <= curLayer.property("Contents").numProperties; i++)

 

instead of:

 

for (i = 1; i <= curLayer.numProperties; i++)

 

And how can I make it work with the actual property from this list ( in this case, ADBE Vector Shape - Rect ) and not the name the user put in ( in this case, "Rectangle 1" )

I tried that but it didn't work:

 

if (curLayer.property("Contents").property(i) == "ADBE Vector Shape - Rect")

 


Again, thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 22, 2021 Nov 22, 2021

Haven't tested it, but I believe it's like this:

 

if (curLayer.property("Contents").property(i).matchName == "ADBE Vector Shape - Rect")
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 22, 2021 Nov 22, 2021
LATEST

Tested that yesterday, it didn't work

I realised that the "ADBE Vector Shape - Rect" isn't the matchName of the folder called " Rectangle 1"

The matchName of the folder is "ADBE Vector Group" and the rectangle property inside is called "ADBE Vector Shape - Rect"

so the for loop should be running thru every "ADBE Vector Group" and check if there's a property called "ADBE Vector Shape - Rect"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines