How to select paths with no name in sublayers?

New Here ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

I'm new in scripting and I want to refer to paths in sublayers. But there are a lot of kind of the same files so we can't give al the paths a name. Is there an option to refer to them while they still have no name? Actually we need all the rectangle paths in two sublayers(NORZIP and COVZIP) inside SHOW_HIDE mainlayer. We have code to select all rectangles in the mainlayer but we can't specify to only select the rectangles in the two sublayers. Afterwards we want to resize the rectangles, some lay down and some are standing so we have to make a difference between these two. It will already be helpfull if we can select only the rectangles we need.
Thanks in advance

var idoc = app.activeDocument;  
var vars = idoc.variables; 
var myLayer = idoc.layers ["SHOW_HIDE"];
    var mySublayer = myLayer.layers ["NORZIP"];
    var mySublayer2 = myLayer.layers["COVZIP"];

//alert(mySublayer);
//alert(mySublayer2);

idoc.selection=null;
for (var i = 0; i < idoc.pathItems.length; i++)
{
    var ipath=idoc.pathItems[i]
    if(ipath.width<40 && ipath.width>39)
    {
        ipath.selected=true;
    }
}
for (var i = 0; i < idoc.pathItems.length; i++)
{
    var ipath=idoc.pathItems[i]
    if(ipath.height<40 && ipath.height>39)
    {
        ipath.selected=true;
    }
}

script_paths.png

TOPICS
Scripting

Views

78

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

Hello,

You can add check whethere it is pathItem or compoundPathItem. Something like

var _item = app.selection[0]
if(_item.constructor.name == 'PathItem'){
    //Select Item
    alert('Select Item here');
}

Since in my code, item is alreday selected, I show alert. So, you need to add extra if to check type.

Best regards

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

You want to avoid accessing items at the document level at all costs. using idoc.pathItems will give you undesirable results (because it also includes any subItems inside of groups, or compound paths.. and this is virtually never the desired outcome in my experience).

 

The good news is, you access the page items from other places in exactly the same way.. So, if you want to get the pathItems on mySubLayer, you can use the exact syntax you have currently, but replace "idoc" with "mySubLayer".

 

That brings us to the next caveat though. When you access items from a layer, or a group, you'll only get the "top level" items.. So, if you have a layer (mySubLayer) that has two groupItems (which we'll say are composed of 5 pathItems each), and two pathItems... You can get the two pathItems with mySubLayer.pathItems. But if you want to get ALL of the pathItems in the layer, you'll need to access them via each groupItem, like so: mySubLayer.groupItems[0].pathItems

 

Unfortunately, I'm going to have to throw you into the deep end a bit here as someone new to scripting.. But Now.. obviously it's not very helpful if you have to hard code every potential groupItem and loop each one separately... Each sublayer might have a different number of groupItems.. and ::GASP:: each groupItem might have groupItems inside of it (and potentially groupItems inside of those!). So unless you have unflinchingly rigid files that will always have the exact same structure (not very useful or common)... You need a way to get all the pathItems in a given layer regardless of where they are nested inside the layer..

 

Enter RECURSION!

 

Sounds scary. Can indeed be scary.. but it's actually not too different from the code you've already written. It's effectively just a loop that can call itself each time it encounters a groupItem so that no matter how deep your groupItems are nested, it will automatically keep going until it reaches the bottom of the stack. Here's a little pseudocode to show you what the function will do:

 

mySubLayer =  layer/sublayer that you want to search

function findPathItems(mySubLayer)

    loop each pageItem in mySubLayer (important note. we're using pageItem because that will give us paths and groups)

        if pageItem.typename === "PathItem"

            pageItem.selected = true;

        else if pageItem.typename === "GroupItem"

            //since this is a groupItem, we want to look for pathItems inside it... call the findPathItems function and pass in this groupItem

            findPathItems(groupItem)

 

and that's basically it. this function will analyze each pageItem inside whatever layer you pass in, and then find and select all pathItems that are nested inside your desired layer. Here's the actual code:

 

function findPathItems(parent)

{

    for(var i=0, curItem;i<parent.pageItems.length;i++)

    {

        curItem = parent.pageItems[i];

        if(curItem.typename === "PathItem")

            curItem.selected = true;

        else if (curItem.typename === "GroupItem")

            findPathItems(curItem)

    }

}

findPathItems(mySubLay);

 

as for how to differentiate between the "<Path>"s and "<Rectangle>"s.... that's a bit trickier and it depends upon your needs... It's tough to tell from your screenshot what might be different about them.. But if you can send a file and tell me how a user would determine which is the correct one, i might be able to point you in the right direction programmatically.

 

Sorry for the novel above.. But i hope it's helpful. Feel free to ask any questions you have. I know i threw a lot at you.

 

Best,

Dilliam

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

LATEST

Hi @Wouter245025961nw7, if you could attach a sample AI document I think someone will provide exact feedback.

- Mark

Likes

Translate

Translate

Report

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