Skip to main content
Known Participant
May 11, 2017
Question

collect and export all layer and sub layer names to an excel file

  • May 11, 2017
  • 3 replies
  • 5986 views

I have an illustrator file with hundreds of layer and sub layers. I need to extract all the layer and sub layer names for referencing by our programmer. I found this photoshop script and am trying to modify it. But it only outputs the top level layer names. The (nested) sub layers don't outputted.

Would be grateful for a tip on how to modify this so that it outputs all layers and sub (nested) layers.

Thanks!

Gary

here's the script:

LayerExcel.jsx

DESCRIPTION

Save layer names to excel file in desktop folder

**********************************************************/

var theLayers = collectLayers(app.activeDocument, []);

////// function collect all layers //////

function collectLayers (theParent, allLayers) {

      if (!allLayers) {var allLayers = new Array}

      else {};

        var theNumber = theParent.layers.length - 1;

      for (var m = theNumber; m >= 0;m--) {

          var theLayer = theParent.layers;

          OutFoldCSV("~/Desktop/Layer_Data",theLayer.name);

          }

      };

     function OutFoldCSV(App_Path,Layer_name){

        var outfolder = new Folder(App_Path)

            if (outfolder.exists == false){

                outfolder.create();

                var myLogFile = new File(outfolder + "/LayerRef.xls");

                myLogFile.open("a", undefined, undefined)

                myLogFile.write(Layer_name);

                myLogFile.write("\n");

            }

            else{

                var myLogFile = new File(outfolder + "/LayerRef.xls");

                myLogFile.open("a", undefined, undefined)

                myLogFile.write(Layer_name);               

                myLogFile.write("\n");

            }

    }

This topic has been closed for replies.

3 replies

Rocket 77Author
Known Participant
May 16, 2017

So I did what you suggested and made a blank file with several empty layers and the script works fine. Once again, "Gary Error". Happens to me all the time. Evidently some of my layers are not really layers, or better said, they are layers with groups in them. What I don't understand though: if I make sure that all items are ungrouped, the ungrouped items do not really appear as layers after ungrouping. they are list in the layers panel, but aren'T really layers ... ??

Here's an example:

Layer 1,Layer 3,This is a layer,

Layer 4,Layer 7,this is a layer,

Disposition_Dev
Community Expert
Community Expert
May 16, 2017

Yea i totally know what you mean.

This is a feature/bug of illustrator (depending upon who you talk to). You can create a group of items, and then you can delete all the items out of the group and you're left with an empty group. (what on earth is an empty group?! that seems like the kind of thing that might open a black hole and suck up the entire universe!) It doesn't happen every time, and I'm unable to replicate the issue consistently, but it does happen and it wreaks havoc.

Additionally the layers panel, for reasons beyond my comprehension, doesn't give you any visual cues regarding what a particular item is. You'd think a simple icon would be extraordinarily helpful with regards to evaluating the file at a glance. But as it stands, and as you've found out the hard way, a layer and a group both look identical in the layers panel.

Glad you were able to identify the issue though. This poses a new question though. Are the groups important to the data you're trying to output? or do you truly only want the layers?

Rocket 77Author
Known Participant
May 16, 2017

I agree completely, this is something that Adobe should consider!

what we are working on is we're building a browser based software for dentists. I am responsible for creating the graphics. These graphics are being exported first using the AI > HTML5 plug-in. The programmers will use the code and the layer names to build the functions in the app.

So that they have a reference as to what layer names are used, etc. I thought it would be handy to export the whole thing to a CSV file.

So the short answer is yes, the groups (and the respective names) are important.

here's a quick look at the app ...

Rocket 77Author
Known Participant
May 15, 2017

Hi Steve,

Thanks for taking the time to help me out. I tried your script. For some strange reason it doesn't seem to read all sublayers.

Here is a test with 2 different AI files. Please note that both files have the same layers and layer naming.

test1.png shows the layers as they are in the AI file.

The script outputs 3 layer names as shown here:

zahn-12,nummer-markierung,wurzel-befunde,

test2.png shows the layers as they are in the AI file. The script outputs 7 layer names as shown here:

zahn-47,nummer-markierung,teilkrone-oben,teilkrone-rechts,teilkrone-unten,teilkrone-left,wurzel-befunde,

I did double check to make sure that none of the layers have commas in their names, if that's any help.

Thanks again for all your help! If you have the time to fix this let me know - I would be glad to pay you for your time!

Thanks,

Gary

Disposition_Dev
Community Expert
Community Expert
May 15, 2017

Hey Fred,

Is it possible for you to send those AI files? My guess is that some of those things might be groups instead of layers, But i can't know unless i see the composition of the file.

Try selecting all of the artwork and deleting it. Then check whether the layers panel looks identical. If there are any groupItems in there, the layers panel will appear different because they will have been deleted.

Groups and layers appear almost identical in the layers panel, (and it's possible, though not likely) to have a groupItem that contains no artwork. Typically this is a remnant of some other script that has been executed. I've encountered that in my own work but i was unable to replicate that behavior using the GUI.

Rocket 77Author
Known Participant
May 16, 2017

Hi William (sorry for calling you Steve! ),

here is a link to a sample file:

Dropbox - test.ai

Thanks again,

Gary

Disposition_Dev
Community Expert
Community Expert
May 12, 2017

I don't know this for sure, but I would highly doubt you could export a .xls file from illustrator. Microsoft uses a proprietary binary format. You can view this by opening a regular excel file in a text editor. It's a whole lot of complete jibberish.

I would suggest writing to your file in CSV format, then you can open that in excel and then save it as a native excel file if you need to.

As to your question about sublayers, in order to dig down and get all the subLayers, you'll need a recursive function to do that. You only loop through the top level layers here. You never even attempt to access the sublayers. I'm not sure how you want the resulting file formatted, so that will be up to you to fix up to your liking, but consider this snippet:

function test()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var result = "";

    function inner(lay)

    {

        for(var y=0;y<lay.layers.length;y++)

        {

            var thisInnerLay = lay.layers;

            result += thisInnerLay.name + ",";

            if(thisInnerLay.layers.length > 0)

            {

                inner(thisInnerLay);

            }

        }

    }

   

    //loop the top level layers

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

    {

        var thisLay = layers;

        result += thisLay.name + ",";

        inner(thisLay);

        result += "\n";

    }

    $.writeln(result);

   

}

test();

Rocket 77Author
Known Participant
May 12, 2017

Thanks, Steve for your help, I'll try this out! Yea, I misspoke myself when I wrote excel. The original script outputs a CSV file. The file looks great - but it only lists the top level layers as you noted.