Copy link to clipboard
Copied
Hi All. I've been all over the internet and haven't seemed to find my solution. Hoping someone on here has a solve.
I'm looking for a script that will delete not only empty layers but will identify and delete empty sub layers. I've found the script thats floating around that is called "DeleteEmptyLayers" but unfortunately it doesn't remove empty sublayers. In this pic it would be "Arrow 6" that needs to be deleted.
I get the sublayers every time I use a symbol then break the symbol then group that object with something else. It leaves behind an empty sublayer and cleaning those up by hand is becoming infuriating. Anyone out there have a fix?
Hi Kristen,
Please give this one a test- let's see if it works:
...#target illustrator
function RemoveEmptySublayers(){
function getNestLevel(layer){
var nestLevelCounter = 0;
function getParent(layer){
if(layer.parent.typename == "Document"){
return false;
}
nestLevelCounter++;
return getParent(layer.parent);
};
getParent(layer);
return nestLevelCounter;
};
function getLayersRecursive
Copy link to clipboard
Copied
Hi Kristen,
Please give this one a test- let's see if it works:
#target illustrator
function RemoveEmptySublayers(){
function getNestLevel(layer){
var nestLevelCounter = 0;
function getParent(layer){
if(layer.parent.typename == "Document"){
return false;
}
nestLevelCounter++;
return getParent(layer.parent);
};
getParent(layer);
return nestLevelCounter;
};
function getLayersRecursive(subjectLayer){
var thisLayer;
for(var i=0; i<subjectLayer.layers.length; i++){
thisLayer = subjectLayer.layers;
allLayers.push({nestLevel : getNestLevel(thisLayer) , layer : thisLayer});
getLayersRecursive(thisLayer);
}
};
function isSublayerDevoidOfNestedArt(layer){
// can only work backwards through a collection of layers sorted by their nested level.
if(layer.parent.typename == "Document"){
return false;
}
if(layer.pageItems.length > 0){
return false;
}
if(layer.pageItems.length == 0 && layer.layers.length == 0){
return true;
}
return false;
};
if(app.documents.length == 0){
return;
}
var doc = app.activeDocument;
var allLayers = [], nestLevel = 0;
getLayersRecursive(doc);
allLayers.sort(function(a, b){return a.nestLevel > b.nestLevel});
var currentSublayer;
for(var i=allLayers.length - 1; i > 0; i--){
//alert(allLayers.layer.name + " " + allLayers.nestLevel);
currentSublayer = allLayers.layer;
if(isSublayerDevoidOfNestedArt(currentSublayer)){
currentSublayer.remove();
}
};
};
RemoveEmptySublayers();
Copy link to clipboard
Copied
Can I use this in Adobe Illustrator CC? Version 2017.0.2....save snip as .js and .jsx and they both do nothing when I try to open with file > Scripts > Other Scripts.
Copy link to clipboard
Copied
if you have more then one version of illustrator then the target line at the top may cause issues.
try running it from the ESTK.
let me know if that means nothing...
here is another one by KELSO back in 2007.
illustrator-scripts/DeleteEmptyLayers.jsx at master · nvkelso/illustrator-scripts · GitHub
Copy link to clipboard
Copied
If they do nothing, are you sure that you are dealing with sublayers and not groups, and that the sublayers are indeed empty?
Copy link to clipboard
Copied
I have two versions of Illustrator on my machine and it's still working well for me. Vasily may have been on the right course suspecting that maybe it wasn't an issue with sublayers.
Copy link to clipboard
Copied
Of course, my personal way to deal with issue is grouping the symbol before breaking the link, so that a sub-layer does not appear. Grouping and pressing Break Link is a couple of separate actions, but they can be recorded as an action such as in my screenshot here:
And, if you care for the symbol name being transferred to the resulting group, a script would be needed in this case which does the same thing except remembers and writes the symbol's name.
Copy link to clipboard
Copied
Hey @Katz.Kristen
Following Script delete empty layers and Sublayers at the same time
function main() {
try {
var doc = app.activeDocument;
}
catch (e) {
alert ('No hay un documento activo');
return
};
var emptyLayers = [];
getEmptyLayers (doc, emptyLayers);
for (var i=0; i<emptyLayers.length; i++) {
emptyLayers[i].remove();
}
}
function getEmptyLayers(container, arr) {
var layers = container.layers;
for (var ii=0; ii<layers.length; ii++) {
try {
var ilayer = layers[ii];
ilayer.canDelete = true;
if (ilayer.layers.length>0) {
getEmptyLayers (ilayer, arr)
}
if (ilayer.pageItems.length==0 && ilayer.canDelete) {
arr.push(ilayer);
}
else {
ilayer.canDelete = false;
container.canDelete = false;
}
}
catch(e){
}
}
}
main();
Best Regards
Gersson
Copy link to clipboard
Copied
that script looks suspiciously familiar
Copy link to clipboard
Copied
Yes, I have a similar script, but that starts like this:
// script.name = deleteEmptyLayers,jsx
// script.description = deletes empty layers and sublayers. If a sublayer is not empty, its parent empty layer will not be removed.
// script.requirement = an open illustrator
// script.parent = CarlosCanto // 10/14/2017
// script.support = canto29@yahoo.com;
// usage - open Illustrator before running the script
Copy link to clipboard
Copied
Hey @CarlosCanto
I´ve taken this script from Internet! it doesn´t have credits, not sure who is Developer´s creator
but anyway I really apogize for this inconvenient 😞
Postdata: I never sad it was made by me!
Best Regards
Gdelgado
Copy link to clipboard
Copied
I've seen enough good scripts from a member named @CarlosCanto to recognize his "handwriting".
Copy link to clipboard
Copied
@pixxxelschubser as you can see my profile, Im beginner 🙂
and at this moment is not possible to detect handwriting, But I really apologize with you as well ! 🙂
Copy link to clipboard
Copied
hey Gesson, don't worry about it too much, but yeah, as pixxxel mentioned, it is highly recommended keeping source comments in code found on the internet. (not saying you removed them).
Copy link to clipboard
Copied
😄
Perhaps a tip for the future: when you publish scripts (from others), try to include the name of the author or the source (link). This is the only recognition we can give to the tireless scriptwriters who share their highly specialized knowledge for free.
Copy link to clipboard
Copied
As well as Ton I have something similar, but it starts a bit different:
// script.name = deleteEmptyLayers,jsx
// script.description = deletes empty layers and sublayers. If a sublayer is not empty, its parent empty layer will not be removed.
// script.requirement = an open illustrator
// script.parent = CarlosCanto
// script.fate = may be scrounged by rascals
// usage - open Illustrator before running the script
Copy link to clipboard
Copied
hahaha you're the best Kurt