Copy link to clipboard
Copied
Hello,
I'm trying to find a script that can export sublayers to JPG files. I did find some scripts that work but they only look at the top-layers, not at the sublayers.
My Illustrator files looks like this:
What I want is a a script that will export the sub-layers in the layer Print into individual JPG files. The filename will be the name of the file itself + the layer name of that specific layer. So Filename_Option 1.jpg etc... Sometimes we have a second layer with sublayers that also needs to be exported to JPG's. I'm thinking to make an Action that first will delete the layers Languages, Working sheet and Background leaving only the layers in place that needs to be exported. But I can't find a script that can export the sub-layers.
This is the script I could find that almost does the job: Scripting Illustrator: Export Layers as Images
But this script only exports the layer Print and does not look at the sublayers in it.
I hope someone can help!
Jeffrey
Copy link to clipboard
Copied
Moving to Illustrator Scripting​
Copy link to clipboard
Copied
Try this script out, it exports a jpeg with all default options.
#target illustrator
function test(){
var doc = app.activeDocument;
var destPath = (doc.path != "")? doc.path : "~/Desktop";
var hiddenTopLayers = [], thisTopLayer;
for(var i=0; i<doc.layers.length; i++){ // hide all visible top-level layers
thisTopLayer = doc.layers;
if(thisTopLayer.name != "Print" && thisTopLayer.visible == true){
thisTopLayer.visible = false;
hiddenTopLayers.push(thisTopLayer);
}
};
var printLayer = doc.layers.getByName("Print"), thisSubLayer, thisSubLayer2;
for(var i=0; i<printLayer.layers.length; i++){ // go through every "Print" sublayer and hide all others to show only the one
thisSubLayer = printLayer.layers;
for(var j=0; j<printLayer.layers.length; j++){
thisSubLayer2 = printLayer.layers
if(j == i){
thisSubLayer2.visible = true;
} else {
thisSubLayer2.visible = false;
}
};
// export the file with all default options
doc.exportFile(File( destPath + "/" + doc.name.replace(/\.\w{2,3}$/, '') + "_" + thisSubLayer.name ), ExportType.JPEG, new ExportOptionsJPEG());
};
for(var i=0; i<doc.layers.length; i++){ // make visible all previously-hidden top-level layers
doc.layers.visible = true;
};
};
test();
Copy link to clipboard
Copied
Thanks but this script doesn't seem to work. I tried it in Illustrator CS4 and in the newest Illustrator CC. I'm not into the scripting.
I just pasted this into a .jsx script file and then went to Illustrator > File > Scripts > Other Script to load this. Maybe I'm doing something wrong?
Copy link to clipboard
Copied
Hmm, I did notice a mistake at the end of the script, which should not affect the general ability though. Do you have your "Print" layer in the document? And, is it saved - because if it's new and not saved, the files will be exported to the desktop.
Copy link to clipboard
Copied
Unfortunately I still can't get it to work...
Silly-V schreef
Do you have your "Print" layer in the document? And, is it saved - because if it's new and not saved, the files will be exported to the desktop.
Yes, the Print layer is in the document and the document is saved. Then I try to load the script but nothing happens. No files on my desktop...
Copy link to clipboard
Copied
So are you sure that what you have inside of the Print layer are sub-layers and not just groups?
Copy link to clipboard
Copied
How can I check the difference? If I unfold a ''sub-layer/group'' in the Print layer this is what I get:
So as you can see there are multiple objects inside the ''sub-layers/groups'' inside the Print layer but I don't know how to see the difference between a sub-layer or a group.
Copy link to clipboard
Copied
I just read this:
''Layers and sublayers appear with a grey background in the layer window. Groups appear with a white background in the layer window. But they both seem to have an identical behaviour.''
Source: Adobe Illustrator - What is the difference between a Layer and a Group? - Stack Overflow
As you can see the background of the ''sub-layers'' are white meaning they are Groups (I think)
Copy link to clipboard
Copied
Okay give this one a try now.
#target illustrator
function test(){
var doc = app.activeDocument;
var destPath = (doc.path != "")? doc.path : "~/Desktop";
var hiddenTopLayers = [], thisTopLayer;
for(var i=0; i<doc.layers.length; i++){ // hide all visible top-level layers
thisTopLayer = doc.layers;
if(thisTopLayer.name != "Print" && thisTopLayer.visible == true){
thisTopLayer.visible = false;
hiddenTopLayers.push(thisTopLayer);
}
};
var printLayer = doc.layers.getByName("Print"), thisSubLayer, thisSubLayer2;
for(var i=0; i<printLayer.groupItems.length; i++){ // go through every "Print" sublayer group and hide all others to show only the one
thisSubLayer = printLayer.groupItems;
for(var j=0; j<printLayer.groupItems.length; j++){
thisSubLayer2 = printLayer.groupItems
if(j == i){
thisSubLayer2.hidden = false;
} else {
thisSubLayer2.hidden = true;
}
};
// export the file with all default options
doc.exportFile(File( destPath + "/" + doc.name.replace(/\.\w{2,3}$/, '') + "_" + thisSubLayer.name ), ExportType.JPEG, new ExportOptionsJPEG());
};
for(var i=0; i<hiddenTopLayers.length; i++){ // make visible all previously-hidden top-level layers
hiddenTopLayers.visible = true;
};
};
test();
Copy link to clipboard
Copied
Thanks a lot for your help so far!
But with this script I'm getting an error (tried it in CS4 and CC):
Copy link to clipboard
Copied
Hmm, can you post a link to your file? I want to be able to do a test on it.
Copy link to clipboard
Copied
No problem, you can download two test AI files with this link:
(files will automatically be deleted in 7 days)
Copy link to clipboard
Copied
OH, I think that's cause you've got "print" and my script , at least one part of it works with it being case-sensitive, so it was looking for "Print". For those who are not aware, the getByName() function is case-insensitive - so the script failed only due to the very first part of the routine where it hides all layers not named "Print". It does so by comparing names exactly, so because "print" isn't "Print", the "print" layer got hidden. The hiding is so any other layers that are not the "Print" will be out of view when a JPG is exported, and at the very end those layers which were hidden get un-hidden again to return to the original view. Well, since the layer we want to work on ("print") got hidden, the rest of the script fails because you can't mess with items of a layer which is locked or hidden (I'm sure there's various situations when this is true & not, for example you can probably rename something via script inside a hidden layer... So - what can you do, you can either stick to calling them "Print", or just replace all "Print" with "print" in the script. And yes it's definitely groups, not sub-layers - which could also be confusing at first because of the colloquial tendency to refer to any art object inside a top-level layer as "sublayer" whereas those objects could be groups and paths and images, etc.
Copy link to clipboard
Copied
Great!! It works, thanks a lot! I'm almost there, two more questions and if that works it's PERFECT
Question 1
Is there any way the export quality of the exported JPG can be set? Now it's exported at (low quality) 72DPI. For me it works better to get 300DPI JPG files. Where can this be changed?
Question 2
Also, in the second test file I send you you can see a few other layers+sublayers that needs to be exported.
Now, the script only looks at the Print layer. Is there a way you tell the script that he has to export every single layer+sublayer except Languages, Working sheet & Background ? Because sometimes we have files that have look like that also. So more than the Print layer.
Now that I'm starting to understand how the script works, I think that when the script is told that Languages, Working sheet & Background should be hidden and the other layers should be exported it should work right? Because these 3 layers are always there and always shouldn't be exported.
Copy link to clipboard
Copied
I'm hoping you can answer my last question. Because that's the only thing that I would like to have changed. After that the script is perfect. Thanks for your help so far!
Copy link to clipboard
Copied
Hello there, say, can you upload a file that's a typical one which describes your 2nd question situation for my testing purposes please?
Copy link to clipboard
Copied
Sure!
Download link: https://we.tl/sK9yOKPNyo (files will automatically be deleted in 7 days)
Which contains:
Test file-1.ai is the exact same one which I'm describing in my 2nd question.
As you can see I also added two extra test files so you have enough files to try the script on.
Copy link to clipboard
Copied
OkY, I got quite a bit of work to get through, hopefully can squeeze this in sometime soon.
Copy link to clipboard
Copied
Great! No rush man! Just look at it when you have time, no problem!
I'm already very thankful for all your help!
Copy link to clipboard
Copied
Silly-V Not trying to rush you but did you had a chance to look at this?
Copy link to clipboard
Copied
Hey, I think their inbox thing is broken - I can't access or read or click on any of your communication messages
Guess we'll have to chat right here. Can you please put the link back up, I need to DL the files on my other computer, sorry for the delay!
Copy link to clipboard
Copied
Oh no problem! Thanks for answering anyway
This is the new link with the same files as the first download link!
Which contains:
Test file-1.ai is the exact same one which I'm describing in my 2nd question.
As you can see I also added two extra test files so you have enough files to try the script on.
Copy link to clipboard
Copied
Silly-V​ Have you downloaded the files already? Because the link is almost expired.
Copy link to clipboard
Copied
Yes, I got them all on all my machines. Sorry I've been caught up with this thing I have to get ready before I go to this convention, for a 5-minute demonstration among the people who are tons and tons smarter than I! But I'm working on this on my home computer, I'll get to it asap.