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

Retriev number of layers

People's Champ ,
Aug 30, 2009 Aug 30, 2009

Copy link to clipboard

Copied

Hi,

I am trying to get the total number of layers on a document but without any consistent results so far.

I tried this but it doesn't work efficiently.

As a layer may contain one or several layers, each one of them may contain also one or multiple sub layers, I have tried a recursive function.

But actually, if a layer does not contain any sublayer, the global function stops. Other layers are ignored and the global amount of layers return uncorrect.

Any tip ? TIA Loic

function slcount(x,sln)
{

    //if sln is not defined on the function call, set sln to zero.
    if(!(sln))
    {
        var sln=x.layers.length;
    }

   
    for(i=0; i<x.layers.length; i++)
    {

//check for any sublayer of the x object if there are sublayers  


        if(x.layers.layers.length>0)
        {

//if sublayers are present, launch recursive call on the new sublayer
            sln+=x.layers.layers.length;
            x=x.layers;
            slcount(x,sln);
        }
        else
        {
            continue;
        }
    }
    return sln;
}


x=slcount(app.activeDocument);
x

TOPICS
Scripting

Views

961

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 Expert ,
Aug 31, 2009 Aug 31, 2009

Copy link to clipboard

Copied

You might have to work on a copy of the file and use the release to layers command from the layers flyout menu in an action and then count the layers.

Votes

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
People's Champ ,
Aug 31, 2009 Aug 31, 2009

Copy link to clipboard

Copied

Hi Larry,

That's very kind of you taking time to answer.

However, maybe, I have misunderstood you but I am not sure how I could use your tip.

I saw that the release to layers command can create layers but my wish is to get the amount of all the layers.

Here is a screenshot. Illustrator gives 3 top layers but there is 8 layers.

I want to get that amount but as you can see a layer may contain a lots of layers spread in many levels.

Do I misuse your advice ?

Image 1.png

Votes

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
Community Expert ,
Aug 31, 2009 Aug 31, 2009

Copy link to clipboard

Copied

I just tried and found it doesn't work exactly like I thought. I was under the impression that the release to layers command created single top level layers for each object.

Upon reading the Layer object has a layers property (read-only) which is the number of sublayers contained in the layer. Maybe you can make use of this property.

Votes

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
People's Champ ,
Sep 01, 2009 Sep 01, 2009

Copy link to clipboard

Copied

LATEST

Hi all,

For one interested, a friend of mine wrote that really efficient code :

function count_layers(layer)
{
  var n_layers = layer.layers.length ;

  // Short circuit
  if (0 == n_layers) return n_layers ;

  for (var i = 0 ; i < layer.layers.length ; i++)
    n_layers += count_layers(layer.layers) ;

  return n_layers ;
}

var layers_in_document = count_layers(app.activeDocument) ;
alert("There are " + layers_in_document + " layers in this document") ;

it returns the global amount of layers.

Regards,

Loic

Votes

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