Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to delete specific layers?

New Here ,
Apr 16, 2015 Apr 16, 2015

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

TOPICS
Scripting
6.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Apr 16, 2015 Apr 16, 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 16, 2015 Apr 16, 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(); 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 16, 2015 Apr 16, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 17, 2015 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 17, 2015 Apr 17, 2015

Line 31  can be changed to:

    doc.close( SaveOptions.SAVECHANGES );

to avoid a dialogue box.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 17, 2015 Apr 17, 2015

‌good to know. Thanks.

i Was not getting any dialog box when I was running it.

may be due to only running it from ESTK

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 19, 2019 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 19, 2019 May 19, 2019
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines