17 Replies Latest reply on Jul 2, 2014 6:43 PM by HarveyLiu

    Script for batch delet redundant layer

    HarveyLiu Level 1

      I have thousands files in the different folders, each file has same layer named “Job Info”. Can someone help me to write a script for del this layer folder by folder?

       

      layerInfo.jpg

       

      I expected the script tells me that I have to select a folder, then it can automatically open the files, del “job info” layer then save file, rewrite, close file

        • 1. Re: Script for batch delet redundant layer
          Chinnadk Level 4

          Hi,

           

          Try this.

           

          var folder = Folder.selectDialog ("Select the root folder");
          var myfiles = [];
          getsubfolderfiles(folder);
          for(var i =0;i<myfiles.length;i++)
          {
                  var doc = app.open(myfiles[i],false);
                  var _layer = doc.layers.item("Job Info");
                  _layer.remove();
                  doc.save();
                  doc.close();
              }
          function getsubfolderfiles(folder)
          {
              var filelist = folder.getFiles();
              for(var i =0;i<filelist.length;i++)
              {
                  if(filelist[i] instanceof Folder)
                  {
                      getsubfolderfiles (filelist[i]);
                  }
                  else if(filelist[i] instanceof File)
                  {
                      if(filelist[i].name.indexOf(".indd") > -1){
                          myfiles.push(filelist[i]);
                      }
                  }
              }
          }
          

          Regards,

          Chinna

          • 2. Re: Script for batch delet redundant layer
            HarveyLiu Level 1

            Great, helpfull! thank you so much, appreciate!

            • 3. Re: Script for batch delet redundant layer
              ASHOK KUMAR Level 1

              Hi Chinna

               

               

              Thanks for the your information, but how can we add additional 3 layers?

               

               

              Thanks

              ASHRAM

              • 4. Re: Re: Script for batch delet redundant layer
                Chinnadk Level 4

                Hi Ashram,

                 

                Try this.

                 

                var folder = Folder.selectDialog ("Select the root folder"); 
                var myfiles = []; 
                var _layers = ["Job Info", "test1", "test2", "test3"];
                getsubfolderfiles(folder); 
                for(var i =0;i<myfiles.length;i++) 
                { 
                        var doc = app.open(myfiles[i],false); 
                        for(var j=0;j<_layers.length;j++)
                        {
                            try
                            {
                                doc.layers.item(_layers[j]).remove(); 
                            }
                            catch(e){}
                        }
                        doc.save(); 
                        doc.close(); 
                    } 
                function getsubfolderfiles(folder) 
                { 
                    var filelist = folder.getFiles(); 
                    for(var i =0;i<filelist.length;i++) 
                    { 
                        if(filelist[i] instanceof Folder) 
                        { 
                            getsubfolderfiles (filelist[i]); 
                        } 
                        else if(filelist[i] instanceof File) 
                        { 
                            if(filelist[i].name.indexOf(".indd") > -1){ 
                                myfiles.push(filelist[i]); 
                            } 
                        } 
                    } 
                }
                

                 

                Regards,

                Chinna

                • 5. Re: Script for batch delet redundant layer
                  ASHOK KUMAR Level 1

                  Hi Chinna

                   

                  Thanks for your script.

                   

                  But there is NO layers added in particular folder Indesign file.

                   

                   

                  Thanks

                  ASHRAM

                  • 6. Re: Script for batch delet redundant layer
                    HarveyLiu Level 1

                    Hi, Chinna

                     

                    here is a script for remove missing link oobject:

                     

                    var link, image, frame, 

                    doc = app.activeDocument, 

                    links = doc.links, 

                    counter = 0; 

                     

                    for (var i = links.length-1; i >= 0; i--) { 

                        if (links[i].status == LinkStatus.LINK_MISSING) { 

                            try { 

                                link = links[i]; 

                                image = link.parent; 

                                frame = image.parent; 

                                frame.remove(); 

                                counter++; 

                            } 

                            catch (err) { 

                                $.writeln(i + " - " + err); 

                            } 

                        } 

                    }

                     

                     

                    is that possible to combine the function into remove layer?

                     

                    I expected the script tells me that I have to select a folder, then it can automatically open the files, del “job info” layer, remove missing link object then save file, rewrite, close file

                    • 7. Re: Script for batch delet redundant layer
                      ASHOK KUMAR Level 1

                      Hi Chinna

                       

                      Any update?

                       

                      Thanks

                      ASHRAM

                      • 8. Re: Re: Script for batch delet redundant layer
                        Chinnadk Level 4

                        Hi Ashram,

                         

                        Sorry, I misunderstood your question. Please try the below.

                         

                        var folder = Folder.selectDialog ("Select the root folder");
                        var myfiles = [];
                        var _layers = ["test1", "test2", "test3"];
                        getsubfolderfiles(folder);
                        for(var i =0;i<myfiles.length;i++)
                        {
                                var doc = app.open(myfiles[i],false);
                                for(var j=0;j<_layers.length;j++)
                                {
                                    try
                                    {
                                        app.activeDocument.layers.add({name:_layers[j]})
                                    }
                                    catch(e){}
                                }
                                doc.save();
                                doc.close();
                            }
                        function getsubfolderfiles(folder)
                        {
                            var filelist = folder.getFiles();
                            for(var i =0;i<filelist.length;i++)
                            {
                                if(filelist[i] instanceof Folder)
                                {
                                    getsubfolderfiles (filelist[i]);
                                }
                                else if(filelist[i] instanceof File)
                                {
                                    if(filelist[i].name.indexOf(".indd") > -1){
                                        myfiles.push(filelist[i]);
                                    }
                                }
                            }
                        }
                        
                        

                         

                        Hi HarveyLiu,

                         

                        Try this,

                         

                        var folder = Folder.selectDialog ("Select the root folder"); 
                        var myfiles = []; 
                        getsubfolderfiles(folder); 
                        for(var i =0;i<myfiles.length;i++) 
                        { 
                                var doc = app.open(myfiles[i],false); 
                                try{
                                    var _layer = doc.layers.item("Job Info");
                                    _layer.remove();
                                    var link, image, frame,
                                    links = doc.links, counter = 0;
                                    for (var i = links.length-1; i >= 0; i--) {
                                        if (links[i].status == LinkStatus.LINK_MISSING) {
                                            try {
                                                link = links[i];
                                                image = link.parent;
                                                frame = image.parent;
                                                frame.remove();
                                                counter++;
                                            }
                                            catch (err) {
                                                $.writeln(i + " - " + err);
                                            }
                                        }
                                    }
                                }
                                catch(e){}
                                doc.save(); 
                                doc.close();
                            } 
                        function getsubfolderfiles(folder) 
                        { 
                            var filelist = folder.getFiles(); 
                            for(var i =0;i<filelist.length;i++) 
                            { 
                                if(filelist[i] instanceof Folder) 
                                { 
                                    getsubfolderfiles (filelist[i]); 
                                } 
                                else if(filelist[i] instanceof File) 
                                { 
                                    if(filelist[i].name.indexOf(".indd") > -1){ 
                                        myfiles.push(filelist[i]); 
                                    } 
                                } 
                            } 
                        }
                        
                        

                         

                        Regards,

                        Chinna

                        • 9. Re: Script for batch delet redundant layer
                          HarveyLiu Level 1

                          Hi, Chinna,

                           

                          Thank you for you help, but the script not work.

                           

                          Regards,

                          Harvey

                          • 10. Re: Script for batch delet redundant layer
                            HarveyLiu Level 1

                            Hi, Chinna,

                             

                            Very sorry,

                             

                            I make a mistake, It works very well, but if the file has not such as "Job Info" layer, it will not remove the missing link object.

                             

                            and:

                             

                            I run the script in indesgn cs3, the it will not tell me that the file has missing object,

                             

                            but:

                             

                            I run it in the cs6, it always tells me that the file has missing object. It is terrible that I have to press enter for the script can keep working

                             

                            Best Regards,

                             

                            Harvey

                            • 11. Re: Re: Script for batch delet redundant layer
                              Chinnadk Level 4

                              Hi Harvey,

                               

                              Replace line numbers 6-9 with below,

                               

                                      var doc = app.open(myfiles[i],false); 
                                      try{
                                          var _layer = doc.layers.item("Job Info"); 
                                              _layer.remove();
                                          }
                                      catch(e{})
                                      try{ 
                              

                               

                              Regards,

                              Chinna

                              • 12. Re: Re: Script for batch delet redundant layer
                                ASHOK KUMAR Level 1

                                Hi Chinna

                                 

                                Now it's working fine. Thanks for your support.

                                 

                                Thanks

                                ASHRAM

                                • 13. Re: Script for batch delet redundant layer
                                  HarveyLiu Level 1

                                  Hi,Chinna

                                   

                                  the Script runs very well, thank you for your helpfull scipt, I save a lot of time by using the script,thank you very much, appreciate!

                                   

                                  But if fix a little problem, it will very perfect.

                                   

                                  Why it keeps giving me a warrning like this:

                                   

                                  missinglink dailog.jpg

                                   

                                  Best Regards,

                                   

                                  Harvey

                                  • 14. Re: Script for batch delet redundant layer
                                    HarveyLiu Level 1

                                    but if it is impossible to fix it, that still a perfect script, I will use it all the time!

                                    • 15. Re: Script for batch delet redundant layer
                                      Chinnadk Level 4

                                      Add this line at the top of the script.

                                      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

                                       

                                      and this at the bottom of the script.

                                      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                                      • 16. Re: Script for batch delet redundant layer
                                        HarveyLiu Level 1

                                        Hi, Chinna,

                                         

                                        the script runs perfectly, thank you for your amazing script. I can use multi computer to run it, it save me a lot of time. thanks

                                         

                                        Harvey

                                        • 17. Re: Script for batch delet redundant layer
                                          HarveyLiu Level 1

                                          Hi, Chinna,

                                           

                                          I got this script which made by Trevor

                                           

                                          var doc = app.properties.activeDocument && app.activeDocument; 

                                          if (!doc) {alert("Adiós Amigo") ; exit();} 

                                          var sN = doc.swatches.everyItem ().name, 

                                              sID = doc.swatches.everyItem ().id, 

                                           

                                              /** UI */ 

                                              w = new Window ('palette', "Print by Color"), 

                                              g1 = w.add('group'), 

                                              g2 = w.add('group'), 

                                              st = g1.add('statictext', undefined, "Select Swatch from Drop Down List") 

                                              d = g1.add('dropdownlist', undefined, sN), 

                                              b1 = g2.add('button', undefined, "PDF"), 

                                              b2 = g2.add('button', undefined, "Print"); 

                                           

                                              b1.onClick = function () {proccessPages (d.selection, true)} 

                                              b2.onClick = function () {proccessPages (d.selection)} 

                                          w.show(); 

                                           

                                          function proccessPages (s, e) { 

                                              var l, c = 0, o, p, pO = {}, pA = []; 

                                              if (s == null) {alert("No Swatch Selected") ; return;}; 

                                              app.findTextPreferences = app.changeTextPreferences = null; 

                                              s = doc.swatches.itemByID (sID[s.index]); 

                                              app.findTextPreferences.fillColor = s; 

                                              var pageFinds = doc.findText (); 

                                              if (!(l = pageFinds.length)) {alert("No finds") ; return;}; 

                                              for (c = 0; c < l; c++) { 

                                                  o = (p = pageFinds[c].parentTextFrames[0].properties.parentPage) && p.documentOffset + 1; 

                                                  if (p) pO["_" + o] = o; // if the finds on a page add to page list 

                                              }; 

                                              c = 0; 

                                              for (o in pO) pA[c++] = pO[o]; 

                                              var pageRange = "+" + pA.join(", +"); 

                                              doc.printPreferences.pageRange = pageRange; 

                                              if (!e) doc.print(true); 

                                              else { 

                                                  app.pdfExportPreferences.pageRange = pageRange; 

                                                  f = File.saveDialog("Export PDF", "PDF: *.PDF"); 

                                                  if (!f) return; 

                                                  if (!f.toString().match(/\.pdf$/i)) f += ".pdf"; 

                                                  doc.exportFile (ExportFormat.PDF_TYPE, f, true); 

                                              } 

                                           

                                          function: find a specify color of text and then gen pdf

                                           

                                          but now:

                                           

                                          I want to change the function of:

                                           

                                          find underline text and then gen pdf is that possible?

                                           

                                          thank you

                                          best regard

                                           

                                          Harvey