Copy link to clipboard
Copied
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");
}
}
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi William (sorry for calling you Steve! ),
here is a link to a sample file:
Thanks again,
Gary
Copy link to clipboard
Copied
haha. no worries at all. I thought it was funny.
I'll have a look at the test file and get back to you.
Copy link to clipboard
Copied
Hallo Gary Vey,
wie dir williamadowling bereits geschrieben hat: stell einfach die zwei Beispieldokumente online. Für einen Test ist nur die Ebenenstruktur wichtig, d.h. du kannst alle Ellemente im Dokument löschen oder (bis hin zur Unkenntlichkeit) verzerren oder auch durch Dummyobjekte ersetzen.
Zum Hochladen eignet sich Dropbox oder xup.in oder irgendein anderer Hoster deiner Wahl (solange man sich dort nicht erst umständlich anmelden muss - das macht nämlich fast niemand )
Copy link to clipboard
Copied
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,
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 ...
Copy link to clipboard
Copied
That's a good looking app. My dentist uses something similar, though not quite that comprehensive.
So if you want, it's possible to include the groups in the data with some alterations, but as someone who's been bitten one too many times by trying to take a quick shortcut like that. My advice is to invest a little bit of time to make the AI file as clean and consistent as possible. You'll thank yourself later. I'm assuming that data will be collected in the browser app and then manipulated within illustrator to generate some kind of visual? If the data will be interacting with the illustrator file, it would be highly prudent to standardize the ai file.
Let me know if you'd rather re-work the code to print groups too and we can have a look at that.
Copy link to clipboard
Copied
Thanks for your kind words! I am not going to invest a whole lot of time with this because, as I mentioned earlier, the output to CSV was only an afterthought - I just wanted to be able to give our programmers a little more info in a spreadsheet.
Since a lot of man hours where already put into creating the AI graphics, I don't think it would be worth it to to "unbuild" groups into layers. The complete AI file has about 900 layers.
Copy link to clipboard
Copied
It's hard to know exactly what the correct answer is here because I don't know the workflow or what the programmers will be doing with the information. If you have any other specific questions, We're here to help. Otherwise, best of luck. Looks like you've got a good product in the works. 😃
Copy link to clipboard
Copied
Hey, have you considered using the SVG output format for this purpose?
Copy link to clipboard
Copied
actually the whole thing was/is built with SVGs. The performance was/is so bad that we have to refractor everything and have decided to use HTML5 Canvas with Fabric.js. After doing some tests the speed improvement was good.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Gary+Vey schrieb
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,
Ein kleiner Hinweis:
markierst du in der Ebenenpalette eine Ebene oder eine Unterebene, dann ist in der Ebenenpalette unten rechts das kleine Symbol für „Unterebene erstellen“ aktiv. Markierst du aber eine Gruppe, dann ist dieses Symbol ausgegraut.
A little hint:
If you select a layer or a sublayer in the layers palette, the small symbol for "Create sublayer" is active in the layer palette at the lower right corner. If you select a group, this symbol is grayed out.
Copy link to clipboard
Copied
Hey, that's a good little trick. I usually just select the unknown item with the circle on the right side of the panel, then check the appearance panel which tells you what the item is (path, linked image, group, etc). This works for me because i always have the appearance panel open, but if one's workflow doesn't lend itself to the appearance panel i could see that being inconvenient.
Good to know. Thanks Pixxxel
Copy link to clipboard
Copied
thanks William!
Copy link to clipboard
Copied
super, danke für den Tipp!