3 Replies Latest reply on Jun 26, 2012 12:58 PM by Paul Riggott

    Case Insensitive Layer Acquisition

    dgolberg Level 1

      Ok, here's the issue I have.  I need to find a layer based on it's name.  The layer's name is stored in a variable inside an array.  The stored name is all lower case, but the actual layer's name may have some upper case in it.  I need a way to target that layer regardless of it's case.  In short, how would I convert the code below to find the layer regardless of what case the layer is, as long as it is spelled the same?

       

      array[0] = "texture";
      app.activeDocument.layers.getByName(array[0]);
      

       

      Let's say the layer's name is actually "TeXture".  How would I get it to properly locate the layer while still using the array string as a comparison?

        • 1. Re: Case Insensitive Layer Acquisition
          Michael L Hale Level 5

          I don't think you are going to be able to use getByName. It's easy to ignore case in string matches but getByName expects an exact match. I think you with have to loop through the layers collection to find the layer.

          • 2. Re: Case Insensitive Layer Acquisition
            dgolberg Level 1

            That's what I was afraid of.  Thought it worth asking though.  Thanks for confirming that.

            • 3. Re: Case Insensitive Layer Acquisition
              Paul Riggott Level 6

              Here is one way of doing it....

               

               

              var array = new Array();
              array[0] = "texture";
              var layerName = getNamesPlusIndexs();
              for(var n in layerName){
                  if(array[0].toString() == layerName[n][1].toString().toLowerCase()){
                      selectLayerByIndex(Number(layerName[n][0]));
                      break;
                      }
                  }
              function getNamesPlusIndexs(){ 
              function getLayerNameByIndex( idx ) { 
                  var ref = new ActionReference(); 
                  ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " )); 
                  ref.putIndex( charIDToTypeID( "Lyr " ), idx );
                  return executeActionGet(ref).getString(charIDToTypeID( "Nm  " )); 
              };
                 var ref = new ActionReference(); 
                 ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID("NmbL") ) 
                 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;
                     var layerName = getLayerNameByIndex( i );
                     if(layerName.match(/^<\/Layer group/) ) continue;
              Names.push([[i],[layerName]]);
                 }; 
              return Names;
              }; 
              function selectLayerByIndex(index,add){ 
               add = (add == undefined)  ? add = false : add;
               var ref = new ActionReference();
                  ref.putIndex(charIDToTypeID("Lyr "), index);
                  var desc = new ActionDescriptor();
                  desc.putReference(charIDToTypeID("null"), ref );
                     if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
                    desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
                 try{
                  executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
              }catch(e){}
              };