11 Replies Latest reply on Aug 11, 2016 4:48 AM by Obi-wan Kenobi

    Play with Layers! …

    Obi-wan Kenobi Adobe Community Professional

      Hi all,

       

      Here's the beginning of my script:

       

      var myDoc = app.activeDocument;
      var myLayer_0 = "Headers_0";
      var myLayer_1 = myDoc.layers.item("Headers_1");
      if (myLayer_1.isValid == true)
          {
          myDoc.layers.item(myLayer_1).remove();
          }
      var myLayer_1 = myDoc.layers.item(myLayer_0).duplicate();
      myLayer_1.name = "Headers_1";
      myDoc.layers.item(myLayer_0).visible = false;
      myDoc.layers.item(myLayer_1).visible = true;
      

       

       

      The idea is simple:

      If "Headers_1" already exists, the script removes it, duplicates "Headers_0", calls it "Headers_1" and makes it visible.

      If "Headers_1" doesn't exist, the script duplicates "Headers_0", calls it "Headers_1" and makes it visible.

       

      … But this doesn't work! 

       

      Thanks in advance!

        • 1. Re: Play with Layers! …
          csm_phil Level 4

          Hi Kenobi,

           

          I just modified your code, If its work my understanding is correct, otherwise please explain I'll check and update once again!.

           

           

          var myDoc = app.activeDocument;  
          var myLayer_0 = "Headers_0";  
          var myLayer_1 = myDoc.layers.item("Headers_1");  
          if (myLayer_1.isValid == true)  
              {  
              myLayer_1.remove();  
              myDoc.layers.item(myLayer_0).visible=true;  
              myDoc.layers.item(myLayer_0).locked=false;  
              var myLayer_1 = myDoc.layers.item(myLayer_0).duplicate();  
              myLayer_1.name = "Headers_1";  
              myDoc.layers.item(myLayer_0).visible = false;  
              try{myDoc.layers.item(myLayer_1).visible == true}catch(e){};
              }  
          else{
              myDoc.layers.item(myLayer_0).visible=true;  
              myDoc.layers.item(myLayer_0).locked=false;  
              var myLayer_1 = myDoc.layers.item(myLayer_0).duplicate();  
              myLayer_1.name = "Headers_1";
              myDoc.layers.item(myLayer_0).visible = false;  
              try{myDoc.layers.item("Headers_1").visible = true;}catch(e){};
              }
          
          

           

           

          thx,

          csm_phil

          • 2. Re: Play with Layers! …
            tpk1982 Level 4

            try..catch method will be useful

            • 3. Re: Play with Layers! …
              Obi-wan Kenobi Adobe Community Professional

              Cool! It works fine! Thanks Phil!

               

              Last step: I would like to apply the method "detach()" to the content of the "Header_1" layer (myLayer_1)

              But only on the current pages, not the master pages! 

               

              myDoc.pageItems.everyItem().layers.item(myLayer_1).everyItem().detach();

               

              … Of course, no result! 

              • 4. Re: Play with Layers! …
                Kai Rübsamen Adobe Community Professional

                First of all, why so complicated?

                 

                var myDoc = app.activeDocument;
                var myLayer_0 = myDoc.layers.itemByName("Headers_0");
                var myLayer_1 = myDoc.layers.itemByName("Headers_1");
                if (myLayer_1.isValid) {
                  myLayer_1.remove();
                }  
                myLayer_1 = myLayer_0.duplicate();
                myLayer_1.name = "Headers_1";  
                myLayer_0.visible = false;
                myLayer_1.visible = true;
                

                 

                Second: I do not understand the new question? If you remove the layer before, every page items that belong to this layer are removed too.

                • 5. Re: Play with Layers! …
                  Obi-wan Kenobi Adobe Community Professional

                  Best result! … but I can't find the code to only catch the "Header_1" layer items!

                  The script catches the "Header_0" layer items too!!! 

                   

                   

                  var myDoc = app.activeDocument; 
                  var myVariable = "Header";
                  var myLayer_0 = "Headers_0";    
                  var myLayer_1 = myDoc.layers.item("Headers_1");    
                  if (myLayer_1.isValid == true)    
                      {    
                      myLayer_1.remove();    
                      myDoc.layers.item(myLayer_0).visible=true;    
                      myDoc.layers.item(myLayer_0).locked=false;    
                      var myLayer_1 = myDoc.layers.item(myLayer_0).duplicate();    
                      myLayer_1.name = "Headers_1";    
                      myDoc.layers.item(myLayer_0).visible = false;    
                      try{myDoc.layers.item(myLayer_1).visible == true}catch(e){};  
                      }    
                  else{  
                      myDoc.layers.item(myLayer_0).visible=true;    
                      myDoc.layers.item(myLayer_0).locked=false;    
                      var myLayer_1 = myDoc.layers.item(myLayer_0).duplicate();    
                      myLayer_1.name = "Headers_1";  
                      myDoc.layers.item(myLayer_0).visible = false;    
                      try{myDoc.layers.item("Headers_1").visible = true;}catch(e){};  
                      }  
                  
                  
                      var i, l, page;  
                            
                      for (i = 0, l = myDoc.pages.length; i < l; i++) {  
                          page = myDoc.pages[i];
                          if (page.appliedMaster !== null) {  
                              for (j = 0, k = page.appliedMaster.pageItems.length; j < k; j++) {  
                                  try {  
                                      page.appliedMaster.pageItems[j].override(page);  
                                  } catch(e) {}  
                              }  
                          }  
                          page.pageItems.everyItem().detach();      
                          page.pageItems.everyItem().textVariableInstances.everyItem(myVariable).convertToText();  
                      }  
                  } 
                  
                  • 6. Re: Play with Layers! …
                    Obi-wan Kenobi Adobe Community Professional

                    Kai, you're right! More simple!

                     

                    About the second point, I want to detach the items of the "Header_1" layer on the current page and only them after I (re)create it.

                     

                    With my script last version, I'm not really sure to finally need to duplicate the "Header_0" layer because its content on the master pages is not touched!

                     

                    What I finally need is first, if I have some items on the "Header_0" layer on the current pages, remove them, then detach the content of the "Header_0" layer on the current pages. And finally, convert the variable "Header" to text!.

                    • 7. Re: Play with Layers! …
                      Kai Rübsamen Adobe Community Professional

                      Michel, but we remove Header_1 ? I would suggest, provide a example, with steps (1,2,3) before / after.

                      • 8. Re: Play with Layers! …
                        Obi-wan Kenobi Adobe Community Professional

                        Something is missing!

                         

                        I can't convert the variable [its parent frame is on "Header_1" layer] to text on the current pages!  [tired brain!]

                         

                        var myDoc = app.activeDocument;    
                        var myVariable = "Header";
                        
                        
                        var myLayer_0 = myDoc.layers.itemByName("Header_0");  
                        var myLayer_1 = myDoc.layers.itemByName("Header_1");  
                        if (myLayer_1.isValid) {  
                          myLayer_1.remove();  
                        }    
                        myLayer_1 = myLayer_0.duplicate();  
                        myLayer_1.name = "Header_1";    
                        myLayer_0.visible = false;  
                        myLayer_1.visible = true;  
                        
                        
                        var TotalPages = (myDoc.pages.count());    
                            
                        for(var CurrentPage=0; CurrentPage < TotalPages; CurrentPage++) {    
                             OverrideMasterItems();    
                          
                        function OverrideMasterItems() {  
                            var myLayer = myDoc.layers.item("Header_1");  
                            var allItems = myDoc.pages[CurrentPage].appliedMaster.pageItems.everyItem().getElements();    
                            for(var i=0;i<allItems.length;i++){    
                                try{  
                                    if (allItems[i].itemLayer == myLayer) {  
                                        allItems[i].override(myDoc.pages[CurrentPage]);  
                                        }  // End if
                                }  // End try
                                catch(e){}    
                            }  // End For 
                        }  // End Function
                        }  // End For 
                        
                        
                        for(0; CurrentPage < TotalPages; CurrentPage++) {   
                            myDoc.pages[CurrentPage].textVariableInstances.everyItem(myVariable).convertToText();
                        }    
                        
                        • 9. Re: Play with Layers! …
                          Obi-wan Kenobi Adobe Community Professional

                          Kai,

                           

                          This version seems to work!

                           

                          var myDoc = app.activeDocument;    
                          var myVariable = "Header";
                          
                          
                          var myLayer_0 = myDoc.layers.itemByName("Header_0");  
                          var myLayer_1 = myDoc.layers.itemByName("Header_1");  
                          if (myLayer_1.isValid) {  
                            myLayer_1.remove();  
                          }    
                          myLayer_1 = myLayer_0.duplicate();  
                          myLayer_1.name = "Header_1";    
                          myLayer_0.visible = false;  
                          myLayer_1.visible = true;  
                            
                            
                              var i, l, page;    
                                      
                              for (i = 0, l = myDoc.pages.length; i < l; i++) {    
                                  page = myDoc.pages[i];  
                                  if (page.appliedMaster !== null) {    
                                      for (j = 0, k = page.appliedMaster.pageItems.length; j < k; j++) {    
                                          try {    
                                              page.appliedMaster.pageItems[j].override(page);    
                                          } catch(e) {}    
                                      }    
                                  }    
                                  page.pageItems.everyItem().textVariableInstances.everyItem(myVariable).convertToText();    
                              }    
                          

                           

                           

                          I've not tested in real situations but, associated to another script [ Cross-reference paragraph number! …  ], we can do that:

                           

                          Capture d’écran 2016-08-10 à 23.58.07.png

                           

                          I mean: with 1 click, generate headers with auto-numbering and active grep styles!  Cool! 

                          • 10. Re: Play with Layers! …
                            Kai Rübsamen Adobe Community Professional

                            1. It seems that your indents are a bit messed up > use better spaces instead of tabs

                            2. Your for-loop is wrong > 3 arguments, not 4

                            3. Since you did not provide a test-doc, I have no time to built a doc and test your result. Sorry.

                            • 11. Re: Play with Layers! …
                              Obi-wan Kenobi Adobe Community Professional

                              Hi all,

                               

                              Thanks to Phil and Kai for their answers that fix the first part of this topic! 

                               

                              Waiting for someone who could give you a global answer that I eventually could mark as "correct", you could take a look at my post#9 where you could find something that could work! At your own risks!