Skip to main content
davejem
Participant
April 17, 2015
Question

How to delete specific layers?

  • April 17, 2015
  • 3 replies
  • 6553 views

Hi,

I have a large batch of files (100+) that all have the same layer structure. The top layer is called 'Guides', the next layer is called 'Object", the next layer is called 'Shadow' and it goes on down through another 5 layers. I'd like to create a script that will select delete certain layers (eg. 'Guides') before I save out a new file as part of an action. I'm coming from a background in Photoshop, where this could all be done in actions, but appears you can't select layers in Illustrator (please correct me if I'm wrong!).

Any help in creating a script would be most appreciated!

Dave

This topic has been closed for replies.

3 replies

urielt87568523
Participant
May 19, 2019

Hi Dave,

The script work for you? how to run it? i try to run the script on AI 2019 no response....
Please advice

Thanks in advance

pixxxelschubser
Community Expert
Community Expert
May 19, 2019

You need a layer (not an object, not a sublayer) with name: guides.

This layer will be removed. Works in Illu CC 2019 as well.

Qwertyfly___
Legend
April 17, 2015

not ideal but it works...

// Batch remove layer from files

// Qwertyfly

// 17-4-2015

// -------------------- WARNING ------------------------------------------------------------------------------

// this will overwrite files - make sure you have made copies of the files before running

//------------------------------------------------------------------------------------------------------------------

// these variables need to be updated before running

var workingDirectory = '~/Desktop/~~~Crap/tst';  // Add folder to process here

var fileType = 'ai';  // make sure you update the file type here

var layerToRemove = 'guides'; // and here is the layer you wish removed

var reg = new RegExp("\.("+fileType+")$", 'i');

var dir = Folder(workingDirectory);

var files = dir.getFiles(reg);

var log = files.length + ' Files Processed \n';

for(var i = 0; i < files.length; i++){

    var file = File(dir + '/' + files.name);

    var doc = app.open(file);

    try{

        app.activeDocument.layers.getByName(layerToRemove).remove();

        log += files.name + ' - "'+layerToRemove+'" Layer removed \n';

    }

    catch(err){

       log += files.name + ' - Has no "'+layerToRemove+'" Layer \n';

    }

    doc.close();

    doc = null

}

txtlog = new File(workingDirectory + '/textlog.txt');

var isopen = txtlog.open("w"); //open file for editing 

if (isopen)//test file is open 

   txtlog.seek(0,0);  

   txtlog.write(log);  

   txtlog.close(); 

davejem
davejemAuthor
Participant
April 17, 2015

Wow, thanks for that. I tried the first  answer you gave and that worked nicely on individual files, and tomorrow I'll try this.

Thanks very much!

Dave

Qwertyfly___
Legend
April 17, 2015

no problem.

make sure you make copies of your files first.

I would hate for it to Bug out and make a mess of anything.

PS. not tested on anything but ai files

Qwertyfly___
Legend
April 17, 2015

app.activeDocument.layers.getByName('guides').remove();

or you could act on all open docs.

for(var i = 0; i < app.documents.length; i++){

    app.documents.layers.getByName('guides').remove();

}

the scipt to work on a folder is a bit more work...