3 Replies Latest reply on Dec 14, 2013 2:46 PM by Colin Flashman

    IDCS6 MACOSX JS: "remove unused layer" script doesn't see masters based on masters

    Colin Flashman Adobe Community Professional

      Hello everybody.

       

      I have an old script that I have used many times to remove unused layers. Until today I thought it worked well:

       

       

      var layers = app.documents[0].layers.everyItem().getElements();
      for(var i=layers.length-1;i>=0;i--){
        if(layers[i].pageItems.length==0){
          layers[i].remove();
        }
      }
      

       

      However, today I used the script on a document that had a layer that was only ever used once on a master page that the other master pages were based upon. To my surprise, when the script was run, this specific layer was deleted.

       

      my scripting knowledge is poor, but looking at the script it looks like the script is looking at all layers that have anything in them, and any layer that has no item is to be deleted. however, I think that pageItems only refers to regular pages or master pages, not masters BASED ON other masters.

       

      I think the problem is similar to another that I had recently where I had a script that removed unused masters, but removed masters that other masters were based on, and I think the principles are the same. That thread can be found at:

       

      http://forums.adobe.com/message/5922004#5922004

       

      in short, the script that worked there was as follows:

       

       

      xUnusedMasters(myDoc);
      
      function xUnusedMasters( docRef ) {
      var mpNames = new Object () ;
      var allPages = docRef.pages ;
      for ( var pIndex = allPages.length - 1 ; pIndex >= 0 ; pIndex-- ) {
      mpNames = addMasterName ( mpNames , allPages[pIndex] ) ;
      }
      
      var allMasterPages = docRef.masterSpreads ;
      for ( var mpIndex = allMasterPages.length - 1 ; mpIndex >= 0 ; mpIndex-- ) {
      if ( ! mpNames[allMasterPages[mpIndex].name] ) {
      allMasterPages[mpIndex].remove() ;
      }
      }
      return ; 
      
      function addMasterName ( MPDB , pageRef ) {
      if ( pageRef == null ) { return MPDB ; }
      if ( pageRef.constructor.name == 'MasterSpread' ) {
      MPDB[pageRef.name] = true ;
      }
      return addMasterName ( MPDB , pageRef.appliedMaster ) ;
      }
      }
      

       

      is it possible to modify the above script so that instead of removing unused master pages, it removes unused layers in a similar fashion?

       

      Many thanks,

       

      Colly

        • 1. Re: IDCS6 MACOSX JS: "remove unused layer" script doesn't see masters based on masters
          Jump_Over Level 5

          Hi,

           

          Maybe a little bit different way...:

           

          Array.prototype.filter = function(collection) {
                    var k, i, con, ids = collection.everyItem().id;
                    con: for (k = ids.length - 1; k >=0; k--) 
                              for (i = 0; i < this.length; i++)
                              if (this[i].id == ids[k]) {
                                   ids.splice(k, 1); 
                                   continue con;
                                   }
                    return ids;     // array with unmatched ids from collection
                    }
          
          var 
                    mL = app.activeDocument.pageItems.everyItem().itemLayer,
                    mUnusedLayersID = mL.filter(app.activeDocument.layers),
                    len = mUnusedLayersID.length;
          
          while (len-->0)
                    app.activeDocument.layers.itemByID(mUnusedLayersID[len]).remove();
          

           

          mL is an array with each pageItem's layer;

          filter compares it to doc.layers collections;

          unmatched means unused so are removed.

           

          Jarek

          • 2. Re: IDCS6 MACOSX JS: "remove unused layer" script doesn't see masters based on masters
            DaveSofTypefi Level 2

            Just to clarify: the Layer.pageItems collection does not include page items that are only on master pages -- it doesn't only miss page items on masters based-on other masters. So, if you have page items on master pages that have not been overridden to any live pages you cannot depend on the Layer.pageItems collection to determine whether or not a layer is in use.

             

            Jump_Over's script analyzes the document's pageItems collection, and that includes all page items in the document whether on live or master pages.

             

            Dave

            1 person found this helpful
            • 3. Re: IDCS6 MACOSX JS: "remove unused layer" script doesn't see masters based on masters
              Colin Flashman Adobe Community Professional

              @Jarek

               

              Looks like it is working a treat! Still have to test it on more documents but on the face of it I think this is the way to go.

               

              Thank you very much for your assistance.

               

              Colly