Skip to main content
Inspiring
September 14, 2023
Answered

Illustrator script to delete the layers that contain selected objects

  • September 14, 2023
  • 2 replies
  • 893 views

Hello

 

I have a very complex Illustrator file (architectual, from CAD) to style - and it's a bit of a mess. I need to delete lots of extraneous layers and I'd like to be able to do that by selecting several objects that appear upon those layers and run a script that will then delete all the layers that the selected items are on (and all other objects also on those layers).

 

I've searched far and wide but come across nothing so far - so please help me Adobe Ones, you're my only hope. 😉

Cheers

Rob

This topic has been closed for replies.
Correct answer Kurt Gold

This can be done with a simple action. Here you can download one:

 

Delete Main Layers

 

Instruction:

 

- Download and unzip the action set file.
- In the Actions palette import the action set.
- Select one or a couple of objects on a main layer.
- Run the action.

 

2 replies

Inspiring
September 14, 2023

@Rob.Wood I may be late to the game with your question but I think this script might accomplish what you are looking for.

// Script to Delete Layers Containing Selected Objects
(function () {
    // Check if there is an active document
    if (app.documents.length > 0) {
      // Get the active document
      var doc = app.activeDocument;
      
      // Get the selected objects
      var selectedObjects = doc.selection;
      
      // Check if there are selected objects
      if (selectedObjects.length > 0) {
        // Loop through all layers in reverse order
        for (var i = doc.layers.length - 1; i >= 0; i--) {
          var layer = doc.layers[i];
          
          // Check if any selected object is on this layer
          var deleteLayer = false;
          for (var j = 0; j < selectedObjects.length; j++) {
            if (selectedObjects[j].layer == layer) {
              deleteLayer = true;
              break;
            }
          }
          
          // If the layer should be deleted, remove it
          if (deleteLayer) {
            layer.remove();
          }
        }
        
        // Alert when the process is completed
        alert("Selected objects' layers have been deleted.");
      } else {
        // No selected objects found
        alert("No objects are currently selected.");
      }
    } else {
      // No active document found
      alert("No active document found.");
    }
  })();
  


Hope this Helps!
 

Kurt Gold
Community Expert
Community Expert
September 14, 2023

Are you talking about main layers only or about some more nested constructions?

 

Rob.WoodAuthor
Inspiring
September 14, 2023

Hello

 

It's just the main (top level) layers that  I'm trying to delete.

Cheers

R

Kurt Gold
Community Expert
Kurt GoldCommunity ExpertCorrect answer
Community Expert
September 14, 2023

This can be done with a simple action. Here you can download one:

 

Delete Main Layers

 

Instruction:

 

- Download and unzip the action set file.
- In the Actions palette import the action set.
- Select one or a couple of objects on a main layer.
- Run the action.