7 Replies Latest reply on May 1, 2017 4:29 AM by johny roger

    Script to cycle only trough all the layers in a layerset/group and stop

    johny roger Level 1

      Hello all

       

      i need a script that will cycle only trough all layers contained in a group called "Colors" and stops once the last layer has been reached.

      So far i do have a script that cycles trough ALL layers in the document, but i need it to cycle trough the defined Layerset/Group ONLY.

       

      doc = app.activeDocument;
      front = doc.layerSets.getByName("Front");
      colors = front.layerSets.getByName("Colors");
      cLayer = doc.activeLayer;
      
      
      ////////////////////////////////////////////////////////////////////////////////////
      ///////////////// FUNCTION TO HIDE ALL LAYERS IN LAYERSET "Colors" ////////////////
      ////////////////////////////////////////////////////////////////////////////////////
      
      
      function resetColorsGroup() {
            for (var i = colors.layers.length - 1; i >= 0; i--)
              {
              colors.layers[i].visible = false; // Hide all Layers
              } 
        };
      
      
      ////////////////////////////////////////////////////////////////////////////////////
      /////////////// FUNCTION TO SELECT THE NEXT LAYER EVEN IF NOT VISIBLE //////////////
      ////////////////////////////////////////////////////////////////////////////////////
      
      
      function activ (nLayer_f)
      {
        var check = nLayer_f.visible;
        doc.activeLayer = nLayer_f;
        if (check == false)
        doc.activeLayer.visible = false;
      }
      
      var parL = doc.activeLayer.parent;
      var parLln = parL.layers.length;
      
      for(i=0; i<parLln;)
      {
        if(parL.layers[i]==cLayer)
        {
          try
          {
            nLayer = cLayer.layers[0];
          }
          catch(e)
          {
            if(i!=parLln-1)
            {
              nLayer=parL.layers[i+1];
            }
            else
            {
              upmem = parL;
              while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)
              {
                upmem = upmem.parent;
              }
              if (upmem==doc)
              {
                upmem=upmem.layers[0];
                var lastmem = 1;
              }
              for(k=0;k<upmem.parent.layers.length;)
              {
                if(upmem.parent.layers[k]==upmem)
                {
                  aa=k;
                  if(lastmem==1)
                  {aa=-1;}
                  nLayer=upmem.parent.layers[aa+1];
                  k=upmem.parent.layers.length;
                }
                else {k++;}
              }
            }
          }
          resetColorsGroup(); //Toggle visibility to OFF for all layers in the "Colors" layerset.
          activ (nLayer);
          i=parLln-1;
          var currentLayer = activeDocument.activeLayer;
          currentLayer.visible = !currentLayer.visible; // Make currently selected Layer Visible.
        }
        i++;
      }
      

       

       

      Thanks everyone, got stuck on this

        • 1. Re: Script to cycle only trough all the layers in a layerset/group and stop
          Chuck Uebele Adobe Community Professional & MVP

          Your script seems more complicated that what I assume you want to do. You need a recursive function to loop through all the layers in a layerset if it contains other layersets:

           

          #target photoshop
          var doc = activeDocument;
          var selectedGroup = doc.layers.getByName('Colors');
          doc.activeLayer = selectedGroup;
          loopLayers (selectedGroup)
          function loopLayers(gp){
              for(var i=0;i<gp.layers.length;i++){
                  if(gp.layers[i].typename =='LayerSet'){
                      loopLayers (gp.layers[i]);
                      } 
                  else{
                      gp.layers[i].visible = false;
                      }
                  }
              }
          
          2 people found this helpful
          • 2. Re: Script to cycle only trough all the layers in a layerset/group and stop
            johny roger Level 1

            Hello Chuck (and others)

             

            i've tried to get your script working for what i need. Unfortunately i could not (my fault). Tried so many things now but could not get it working and help is highly appreciated.

             

            I'll try to explain again what i need with some pseudo code:

             

            select First layer in Group "Colors"  (i.E. "Green") and make only this Color/Layer visible.
            {Play function "create"};
            select next layer in Group "Colors" (i.E. "Yellow") and make only this Color/Layer visible.
            {Play function "create"};
            Repeat until all layers in Group "Colors" have been processed with function "create" (Amount and Names of layers in Group "Colors" is not consistent).
            

             

             

            Screenshot_28_04_17__12_19.jpg

            • 3. Re: Script to cycle only trough all the layers in a layerset/group and stop
              JJMack Most Valuable Participant

              The Thing is that all layers in a docent can be processed.  It is usually done using a recursive function that process the document object passed.  If you passed the function a group instead of a document it would process the Group Object all the layers and other groups that may be in within a group.  You could write a function to ignore  groups in the object passes and just process the layers in the group,  However all the layers in the lower groups ignored are logically in the group you processes where the visibility can be controlled by the lower groups and layers visibility settings. 

              • 4. Re: Script to cycle only trough all the layers in a layerset/group and stop
                Chuck Uebele Adobe Community Professional & MVP

                So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?

                • 5. Re: Script to cycle only trough all the layers in a layerset/group and stop
                  johny roger Level 1

                  https://forums.adobe.com/people/Chuck+Uebele  schrieb

                   

                  So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?

                  Thanks Chuck. Not really I try to explain again:

                   

                  1. Group "Colors" contains solids which act as Background for content above the Group.
                  2. Toggle only the Green Background to visible in Group "Colors" and run block of code named "Create" (apply filters and then save a jpg)
                  3. Toggle only the Yellow Background to visible and run the same "Create" code/function again.
                  4. Repeat this step for each Layer available in the "Colors" Group.
                  5. Stop the loop (i.E. "Red") has been processed by the "Create" code/function.

                   

                  Currently i have the following code that will check for the currently visible layer in the "Colors" group and select it.

                  After each repetition it will select the next layer in the "Colors" group and makes only the newly selected visible.

                  Code has been put together from snippets and could be done easier and is not yet ready to work as it should.

                   

                  stepTroughColors()
                  
                  
                  function stepTroughColors(){
                    
                  var doc = activeDocument; 
                  var front = doc.layerSets.getByName("Front");
                  var colors = front.layerSets.getByName("Colors"); 
                  var cLay = doc.activeLayer;  
                  var layNum   
                  var aLay = new Array() 
                  //var i = [0] 
                    
                  for(var i=0;i<colors.layers.length;i++){
                      
                      theLayer = colors.layers[i]; 
                      if(theLayer.visible == true){
                      doc.activeLayer = theLayer; // Sets the visible Layer in "Colors" as active Layer.
                      activeColor = theLayer.name; // Variable "activeColors" to use the Layername in the Save jpg actions.
                      }
                      aLay[i] = colors.layers[i].name; 
                      if(aLay[i]==cLay.name){
                      layNum = i;
                      colors.layers[i].visible = false;
                        }
                        if(doc.activeLayer.name.indexOf("END") >= 0){
                        alert ("All Colors Have Been Processed and Saved!");
                        break;
                        }
                      }; 
                  try{  
                      doc.activeLayer = colors.layers[layNum+1];  
                      doc.activeLayer.visible = true; 
                      alert (activeColor) 
                      }  
                  catch(e){}  
                  };
                  
                  • 6. Re: Script to cycle only trough all the layers in a layerset/group and stop
                    Chuck Uebele Adobe Community Professional & MVP

                    Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.

                     

                    #target photoshop
                    var doc = activeDocument;
                    var selectedGroup = doc.layers.getByName('Colors');
                    doc.activeLayer = selectedGroup;
                    var reset = true;
                    loopLayers (selectedGroup)//turn off visibility
                    reset = false;
                    loopLayers (selectedGroup)//run loop again to run your inserted code
                    
                    function loopLayers(gp){
                        for(var i=0;i<gp.layers.length;i++){
                            if(gp.layers[i].typename =='LayerSet'){
                                loopLayers (gp.layers[i]);
                                }
                            else if(reset){
                                gp.layers[i].visible = false;
                                }
                            else{
                                gpLayers[i].visible = true;
                                //enter code here to save or whatever
                                gpLayers[i].visible = false;
                                }
                            }
                        }
                    
                    • 7. Re: Script to cycle only trough all the layers in a layerset/group and stop
                      johny roger Level 1

                      https://forums.adobe.com/people/Chuck+Uebele  schrieb

                       

                      Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.

                       

                       

                      Hello Chuck

                       

                      thank you so much for your kind help! That did the trick, just had to correct 2 typos (also corrected below).

                      Have a great week and happy to get such a great support here in the forum!

                       

                      Regards

                       

                      Daniel

                       

                      #target photoshop  
                      var doc = activeDocument;  
                      var selectedGroup = doc.layers.getByName('Colors');  
                      doc.activeLayer = selectedGroup;  
                      var reset = true;  
                      loopLayers (selectedGroup)//turn off visibility  
                      reset = false;  
                      loopLayers (selectedGroup)//run loop again to run your inserted code  
                        
                      function loopLayers(gp){  
                          for(var i=0;i<gp.layers.length;i++){  
                              if(gp.layers[i].typename =='LayerSet'){  
                                  loopLayers (gp.layers[i]);  
                                  }  
                              else if(reset){  
                                  gp.layers[i].visible = false;  
                                  }  
                              else{  
                                  gp.layers[i].visible = true;  
                                  //enter code here to save or whatever  
                                  gp.layers[i].visible = false;  
                                  }  
                              }  
                          }