6 Replies Latest reply on Sep 29, 2017 12:52 PM by Katz.Kristen

    Removing Empty Sub Layers.

    Katz.Kristen

      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?  Screen Shot 2015-11-05 at 1.07.15 PM.png

        • 1. Re: Removing Empty Sub Layers.
          Silly-V Adobe Community Professional

          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[i];
                      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[i].layer.name + " " + allLayers[i].nestLevel);
                  currentSublayer = allLayers[i].layer;
                  if(isSublayerDevoidOfNestedArt(currentSublayer)){
                      currentSublayer.remove();
                  }
              };
          };
          RemoveEmptySublayers();
          
          • 2. Re: Removing Empty Sub Layers.
            Silly-V Adobe Community Professional

            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:

            BreakSymbolLinkAction.png

            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.

            1 person found this helpful
            • 3. Re: Removing Empty Sub Layers.
              TrueHarlequin

              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.

              • 4. Re: Removing Empty Sub Layers.
                Qwertyfly... Level 4

                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

                • 5. Re: Removing Empty Sub Layers.
                  Silly-V Adobe Community Professional

                  If they do nothing, are you sure that you are dealing with sublayers and not groups, and that the sublayers are indeed empty?

                  • 6. Re: Removing Empty Sub Layers.
                    Katz.Kristen Level 1

                    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.