9 Replies Latest reply on Aug 11, 2010 8:31 AM by Michael L Hale

    Script that selects specific color coded layer?

    VertexBee

      Hi Guys, does anybody knows how to find a layer in the layers palette using its color coding ?

       

      I've did this action for Photoshop for stacking layers on top of each other,basically it aligns one

      layer to another regardless of their position and size.Here is a demo how it works:  http://vimeo.com/13713886

       

      The action searches for the target layer via its specific name, using a simple .jsx script.This way the layer order in your document doesn't matter.However I don't want the action to rely on layer name either and was thinking that It can find the layer via its color coding

      http://www.vertexbee.com/pics/adobeForum01.jpg

       

       

      I would be really glad if someone can write a script that searches trough all the layers in the layers palette and selects the color coded one.

      Thank you.

        • 1. Re: Script that selects specific color coded layer?
          Paul Riggott Level 6

          This should do it...

          selectAllLayers();
          var selectedLayers =  getSelectedLayersIdx();
          for(var a=0;a< selectedLayers.length;a++){
              makeActiveByIndex( selectedLayers[a], false );
              if(colourMarkedLayer()) break;
              }
          function selectAllLayers() {
          activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
          if(activeDocument.activeLayer.isBackgroundLayer) 
          activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-2];
          var BL = activeDocument.activeLayer.name;
          activeDocument.activeLayer = activeDocument.layers[0];
              var desc5 = new ActionDescriptor();
                  var ref3 = new ActionReference();
                  ref3.putName( charIDToTypeID('Lyr '), BL);
              desc5.putReference( charIDToTypeID('null'), ref3 );
              desc5.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelectionContinuous') );
              desc5.putBoolean( charIDToTypeID('MkVs'), false );
              executeAction( charIDToTypeID('slct'), desc5, DialogModes.NO );
          };
          function getSelectedLayersIdx(){ 
                var selectedLayers = new Array; 
                var ref = new ActionReference(); 
                ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                var desc = executeActionGet(ref); 
                if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 
                   desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 
                    var c = desc.count 
                    var selectedLayers = new Array(); 
                    for(var i=0;i<c;i++){ 
                      try{ 
                         activeDocument.backgroundLayer; 
                         selectedLayers.push(  desc.getReference( i ).getIndex() ); 
                      }catch(e){ 
                         selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 
                      } 
                    } 
                 }else{ 
                   var ref = new ActionReference(); 
                   ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
                   ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                   try{ 
                      activeDocument.backgroundLayer; 
                      selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); 
                   }catch(e){ 
                      selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); 
                   } 
                } 
                return selectedLayers; 
           };
          function makeActiveByIndex( idx, visible ){ 
              var desc = new ActionDescriptor(); 
                var ref = new ActionReference(); 
                ref.putIndex(charIDToTypeID( "Lyr " ), idx) 
                desc.putReference( charIDToTypeID( "null" ), ref ); 
                desc.putBoolean( charIDToTypeID( "MkVs" ), visible ); 
             executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
          };
          function colourMarkedLayer(){
             var ref = new ActionReference(); 
             ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
             var appDesc = executeActionGet(ref); 
             if(appDesc.getEnumerationValue(stringIDToTypeID('color')) != 1315925605) return true;
             return false;
          }
          
          1 person found this helpful
          • 2. Re: Script that selects specific color coded layer?
            VertexBee Level 1

            Hey, Paul, thank you for your quick response !

            I've tested it in CS2 with a bunch of layers and nothing happened.

            I copied all the code in your post, put it in new .jsx in the presets/scripts folder and execute it in CS2, am I doing smth wrong?

            • 3. Re: Script that selects specific color coded layer?
              Paul Riggott Level 6

              Sorry about that, it won't work in CS2, if you don't have any layersets this should work...

              var doc = activeDocument;
              for(var a = 0; a<doc.layers.length;a++){
                  doc.activeLayer = doc.layers[a];
                  if(colourMarkedLayer()) break;
               }
              function colourMarkedLayer(){
                 var ref = new ActionReference(); 
                 ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                 var appDesc = executeActionGet(ref); 
                 if(appDesc.getEnumerationValue(stringIDToTypeID('color')) != 1315925605) return true;
                 return false;
              }
              
              • 4. Re: Script that selects specific color coded layer?
                VertexBee Level 1

                Wow, nice, it works like a charm. Thank you very much !

                I'll update my action to use your script instead.

                One more last thing, almost always my psd documents contain layer sets, is it possible to make it work even with layer sets ?

                • 5. Re: Script that selects specific color coded layer?
                  Paul Riggott Level 6

                  Please try this now...

                  var doc = app.activeDocument; 
                  processLayers (doc) 
                  function processLayers (objectRef) { 
                  var myNumber = objectRef.layers.length; 
                  for (var i = 0; i < myNumber ; i++) { 
                       if(colourMarkedLayer()) break;
                   var myLayer = objectRef.layers[i]; 
                    doc.activeLayer = myLayer;    
                   if (myLayer.typename == 'LayerSet') { 
                    processLayers (myLayer);
                    } 
                   } 
                  } 
                  function colourMarkedLayer(){
                     var ref = new ActionReference(); 
                     ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                     var appDesc = executeActionGet(ref); 
                     if(appDesc.getEnumerationValue(stringIDToTypeID('color')) != 1315925605) return true;
                     return false;
                  }
                  
                  1 person found this helpful
                  • 6. Re: Script that selects specific color coded layer?
                    VertexBee Level 1

                    Man, you're my hero ! Thank you once again for your help! )) The script works great!

                    • 7. Re: Script that selects specific color coded layer?
                      Ghoulfool Level 1

                      Paul,

                       

                      Where did you find the documetation on colourMarkedLayer? I was looking for layer properties info but couldn't find anything appropriate.

                      And sadly  app.activeDocument.layers.getBy("red"); doesn't work

                      • 8. Re: Script that selects specific color coded layer?
                        Paul Riggott Level 6

                        You can get the layer info with this code that will write the keys to ESTK console..

                        var ref = new ActionReference(); 
                        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                        var appDesc = executeActionGet(ref); 
                        var c =appDesc.count;
                         for(var i=0;i<c;i++){
                             $.writeln('Key '+i+' = '+IDTz(appDesc.getKey(i))+' : '+appDesc.getType(appDesc.getKey(i)));
                         }
                        function IDTz(id){ 
                           try { 
                              var res = typeIDToStringID( id ); 
                              if(res == '' ){ 
                                 var res = typeIDToCharID( id ); 
                              } 
                           }catch(e){} 
                           return res; 
                        } 
                        function zTID( s ){ 
                           if( s.length == 4 ) var res = charIDToTypeID( s ); 
                           if( s.length > 4 ) var res = stringIDToTypeID( s ); 
                           return res; 
                        }
                        
                        • 9. Re: Script that selects specific color coded layer?
                          Michael L Hale Level 5

                          To fully explore an ActionDescriptor you may need to also explore ActionList and ActionReference. Here is the full code I use. As I explore the descriptor I shift the block comments to work with the current object. Below explores an ActionList.

                           

                               
                          var ref = new ActionReference();
                          ref.putEnumerated( zTID("Lyr "), zTID("Ordn"), zTID("Trgt") ); 
                          var desc = executeActionGet(ref).getList(zTID('channelRestrictions'));
                          
                          var c = desc.count
                          /*
                          for(var i=0;i<c;i++){ //enumerate reference. use getForm() to determine which get method to use
                            $.writeln('Key '+i+' = '+desc.getReference(i).getIndex()); 
                          }
                          */
                          
                          for(var i=0;i<c;i++){ //enumerate list
                            $.writeln('Key '+i+' = '+desc.getType(i)) 
                          }
                          
                          
                          /*
                          for(var i=0;i<c;i++){ //enumerate descriptor's keys
                            $.writeln('Key '+i+' = '+IDTz(desc.getKey(i))+': '+desc.getType(desc.getKey(i))) 
                          }
                          */
                          function IDTz(id){
                               try {
                                    var res = typeIDToStringID( id );
                                    if(res == '' ){
                                         var res = typeIDToCharID( id );
                                    }
                               }catch(e){}
                               return res;
                          }
                          function zTID( s ){
                               if( s.length == 4 ) var res = charIDToTypeID( s );
                               if( s.length > 4 ) var res = stringIDToTypeID( s );
                               return res;
                          }