6 Replies Latest reply on Apr 17, 2015 8:48 PM by Qwertyfly...

    How to delete specific layers?

    davejem

      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

        • 1. Re: How to delete specific layers?
          Qwertyfly... Level 4
          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[i].layers.getByName('guides').remove();
          }
          

           

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

          • 2. Re: How to delete specific layers?
            Qwertyfly... Level 4

            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[i].name);
                var doc = app.open(file); 
                try{
                    app.activeDocument.layers.getByName(layerToRemove).remove();
                    log += files[i].name + ' - "'+layerToRemove+'" Layer removed \n';
                }
                catch(err){
                   log += files[i].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();  
            }  
            
            • 3. Re: How to delete specific layers?
              davejem Level 1

              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

              • 4. Re: How to delete specific layers?
                Qwertyfly... Level 4

                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

                • 5. Re: How to delete specific layers?
                  elDudereno Level 3

                  Line 31  can be changed to:

                      doc.close( SaveOptions.SAVECHANGES );
                  

                  to avoid a dialogue box.

                  • 6. Re: How to delete specific layers?
                    Qwertyfly... Level 4

                    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