7 Replies Latest reply on Mar 20, 2017 2:41 PM by johny roger

    Check which Layer in this group is visible and store it's name as variable

    johny roger Level 1

      Hello All

       

      does anyone have a script available that checks which Subgroup (or layer) in GROUP A is visible and store it's name as variable to use it later in the script?

       

      1. Checking GROUP A which layer or group inside it is visible (will be only one at a time).

      2. Storing the name of the visible group/layer as variable.

      3. use this variable (together with others) as name for the exported file.

       

      I have a script that checks if a given layer name is visible, but i would want to check a group for a visible layer or subgroup.

       

      1. var doc = app.activeDocument 
      2. var layersToTargetNameArray = ["Background", "Artwork", "diecut", "size", "sign of panel"]; 
      3. for (var i = 0, il = doc.layers.length; i < il; i++) { 
      4.     var curLayer = doc.layers[i]; 
      5.     for (var j = 0, jl = layersToTargetNameArray.length; j < jl; j++) { 
      6.         if (curLayer.name == layersToTargetNameArray[j]) { 
      7.             // -------------------------------------------- 
      8.             // do something if layer name found 
      9.             // -------------------------------------------- 
      10.             curLayer.visible = false; // basic example 
      11.         } else
      12.             // -------------------------------------------- 
      13.             // do something else if layer name not found 
      14.             // -------------------------------------------- 
      15.         } 
      16.     } 

       

       

      Thanks everyone.

        • 1. Re: Check which Layer in this group is visible and store it's name as variable
          Chuck Uebele Adobe Community Professional & MVP

          You need to use a recursive function to search each group and subgroup. The if statement detects if the layer is a layerset and if so restarts the function to search for layers in that group.

           

          var doc = app.activeDocument 
              var layersToTargetNameArray = ["Background", "Artwork", "diecut", "size", "sign of panel"]; 
          
          findLayers (doc)
          function findLayers(gp){
             for (var i = 0, i< gp.layers.length; i++) { 
                  var curLayer = gp.layers[i]; 
                  if(curLayer.typename == 'LayerSet'){findLayers (curLayer)}
                  else{
                      for (var j = 0, jl = layersToTargetNameArray.length; j < jl; j++) { 
                          if (curLayer.name == layersToTargetNameArray[j]) { 
                              // -------------------------------------------- 
                              // do something if layer name found 
                              // -------------------------------------------- 
                              curLayer.visible = false; // basic example 
                          } else { 
                              // -------------------------------------------- 
                              // do something else if layer name not found 
                              // -------------------------------------------- 
                          } 
                      } 
                  }  
              }
          }
          
          1 person found this helpful
          • 2. Re: Check which Layer in this group is visible and store it's name as variable
            johny roger Level 1

            Thank you Chuck

             

            I checked your script and as far as i understand it it does:

             

            • Check if any of the layer names in the array can be found and if they are visible.

             

            It's not exactly what i'm trying to accomplish, but a good start What i try is:

             

            • Check which sub layerset in layerset "Front/Colors" is visible. Inside colors i have dozens of colors and would want to check which one is currently visible so i can use it's name i.E. "Red" as a variable for the save command later on.

             

            My layer structures/stacks in all needed documents are:

             

            +Front

                 +Print Effects

                 +Colors

                      +Red

                      +Blue

                      +Green

            +Back

                 +Print Effects

                 +Colors

                      +Red

                      +Blue

                      +Green

             

            So, if "red" would be visible and i saved this to variable 'color' i could save my file as:

             

            thedoc.saveAs((new File(docPath+'/'+ color + basename + thedoc.activeLayer.name +' .jpg')),jpegOptions,true); 

             

            Does that make sense?

             

            p.s. i'm looking to hire someone for a scripting job, in case you would be available or knew someone   Thanks Chuck

            • 4. Re: Check which Layer in this group is visible and store it's name as variable
              johny roger Level 1

              JJMack, i know this website, but that's not my question

              • 5. Re: Check which Layer in this group is visible and store it's name as variable
                Chuck Uebele Adobe Community Professional & MVP

                I'm not totally clear on what you want. My response was just to show you how to search layers in groups and subgroups. There are people that may want to do this for you, and they should PM you. Right now, I don't really have a lot of time.

                • 6. Re: Check which Layer in this group is visible and store it's name as variable
                  johny roger Level 1

                  Hello Chuck

                   

                  after hundreds of script snippet tests and reading in the forum, i did not find a solution yet. Try to explain again as simple as i can

                   

                  1. I have a layerset called "Colors" which is in fact nested in another layerset called "Front".

                  2. My script should check which layer inside "Colors" (i.e. Red, or Yellow, or Blue) is visible and store its name as a variable.

                  3. When the script saves the jpg, it can append the Name of the visible layer aka Color (i.e. Blue) to my filename.

                   

                  Any idea how this could be done? Thank you for your Time and have a great weekend.

                   

                  Daniel

                  • 7. Re: Check which Layer in this group is visible and store it's name as variable
                    johny roger Level 1

                    So, i have found a script that does select all visible layers in a document. Now i'd need to edit it to select the visible layer inside the layerset named "Colors"

                     

                    How could i "limit" the script to just check the "Colors" layerset for visible layers (will be just one at a time, i.e. "Yellow")?

                     

                    #target photoshop
                    app.bringToFront();
                    
                    
                    main();
                    function main(){
                    if(!documents.length) return;
                    var Vis = getVisLayers();
                    deselectLayers();
                    for(var a in Vis){
                        selectLayerById(Number(Vis[a]),true);
                        }
                    }
                    function getVisLayers(){ 
                       var ref = new ActionReference(); 
                       ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
                       var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
                       var Names=[];
                    try{
                        activeDocument.backgroundLayer;
                    var i = 0; }catch(e){ var i = 1; };
                       for(i;i<count;i++){ 
                           if(i == 0) continue;
                            ref = new ActionReference(); 
                            ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
                            var desc = executeActionGet(ref);
                            var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
                            var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
                            if(layerName.match(/^<\/Layer group/) ) continue;
                            var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
                            var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
                            var vis = desc.getBoolean(charIDToTypeID( "Vsbl" ));
                            if(!isLayerSet && vis) Names.push(Id);
                       }; 
                    return Names;
                    };
                    function selectLayerById(ID, add) {
                        add = (add == undefined)  ? add = false : add;
                       var ref = new ActionReference();
                       ref.putIdentifier(charIDToTypeID('Lyr '), ID);
                       var desc = new ActionDescriptor();
                       desc.putReference(charIDToTypeID('null'), ref);
                       if (add) {
                          desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
                       }
                       desc.putBoolean(charIDToTypeID('MkVs'), false);
                       executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
                    }
                    function deselectLayers() { 
                        var desc01 = new ActionDescriptor(); 
                            var ref01 = new ActionReference(); 
                            ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
                        desc01.putReference( charIDToTypeID('null'), ref01 ); 
                        executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO ); 
                    };