Skip to main content
Known Participant
August 25, 2017
Answered

Apply layer color id based on name

  • August 25, 2017
  • 1 reply
  • 3095 views

Hi, can experts here help me with a script that applies layer color id to red if the layer name has exposure, level and layer?

Finally, a script that removes all the layer color id if there are no more layer names that have exposure, level and layer

This topic has been closed for replies.
Correct answer Davide_Barranca12040269

We are almost there Looks like it's working and keeping the layer red if they are not in a group but it's removing the red color if the layer is in a group.

Here is how it looks, level in group is not red


Fixed a bug with Groups in the second script

First Script (sets Red):

#target photoshop 

app.bringToFront(); 

 

app.activeDocument.suspendHistory('Setting Red colortag', 'main()'); 

 

function main(){ 

if(!documents.length) return; 

selectAllLayers(); 

var selLayers = getSelectedLayersIdx(); 

for(var a in selLayers){ 

        colorLayer(Number(selLayers)); 

    } 

 

function colorLayer(idx){ 

    var ref = new ActionReference(); 

    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "name" )); 

    ref.putIndex( charIDToTypeID( "Lyr " ), idx); 

    var desc = executeActionGet(ref); 

    makeActiveByIndex( idx ); 

    var layerName = desc.getString(stringIDToTypeID('name')); 

    if(layerName.indexOf("Layer")!=-1 || layerName.indexOf("Exposure")!=-1 || layerName.indexOf("Levels")!=-1){ 

        var desc1 = new ActionDescriptor();       

        var ref1 = new ActionReference();       

        ref1.putIndex (stringIDToTypeID ("layer"), idx);       

        desc1.putReference (stringIDToTypeID ("target"), ref1);       

        var desc2 = new ActionDescriptor();       

        desc2.putEnumerated (stringIDToTypeID ("color"), stringIDToTypeID ("color"), stringIDToTypeID ("red"));       

        desc1.putObject (stringIDToTypeID ("to"), stringIDToTypeID ("layer"), desc2);     

        executeAction (stringIDToTypeID ("set"), desc1, DialogModes.NO);  

    } 

 

function makeActiveByIndex( idx ){ 

    var desc = new ActionDescriptor(); 

      var ref = new ActionReference(); 

      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 

      desc.putReference( charIDToTypeID( "null" ), ref ); 

      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 

   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 

}; 

 

 

function selectAllLayers() { 

    var desc29 = new ActionDescriptor(); 

        var ref23 = new ActionReference(); 

        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

    desc29.putReference( charIDToTypeID('null'), ref23 ); 

    executeAction( stringIDToTypeID('selectAllLayers'), desc29, 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; 

}; 

Second script sets the red to those layers, and in case also removes the red from the layers where the strings are missing:

#target photoshop 

app.bringToFront(); 

 

app.activeDocument.suspendHistory('Setting or Removing Red colortag', 'main()'); 

 

function main(){ 

if(!documents.length) return; 

selectAllLayers(); 

var selLayers = getSelectedLayersIdx(); 

for(var a in selLayers){ 

        colorLayer(Number(selLayers)); 

    } 

 

function colorLayer(idx){ 

   

    var ref = new ActionReference(); 

    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "layerKind" )); 

    ref.putIndex( charIDToTypeID( "Lyr " ), idx); 

    var desc = executeActionGet(ref); 

    if (desc.getInteger(stringIDToTypeID("layerKind")) == 7) return;

   

    var ref = new ActionReference(); 

    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "name" )); 

    ref.putIndex( charIDToTypeID( "Lyr " ), idx); 

    var desc = executeActionGet(ref); 

    makeActiveByIndex( idx ); 

    var layerName = desc.getString(stringIDToTypeID('name')); 

    var layerColor = (layerName.indexOf("Layer")!=-1 || layerName.indexOf("Exposure")!=-1 || layerName.indexOf("Levels")!=-1) ? stringIDToTypeID ("red") : charIDToTypeID("None"); 

    var desc1 = new ActionDescriptor();       

    var ref1 = new ActionReference();       

    ref1.putIndex (stringIDToTypeID ("layer"), idx);       

    desc1.putReference (stringIDToTypeID ("target"), ref1);       

    var desc2 = new ActionDescriptor();       

    desc2.putEnumerated (stringIDToTypeID ("color"), stringIDToTypeID ("color"), layerColor);       

    desc1.putObject (stringIDToTypeID ("to"), stringIDToTypeID ("layer"), desc2);     

    executeAction (stringIDToTypeID ("set"), desc1, DialogModes.NO);

     

 

function makeActiveByIndex( idx ){ 

    var desc = new ActionDescriptor(); 

      var ref = new ActionReference(); 

      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 

      desc.putReference( charIDToTypeID( "null" ), ref ); 

      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 

   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 

}; 

 

function selectAllLayers() { 

    var desc29 = new ActionDescriptor(); 

        var ref23 = new ActionReference(); 

        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

    desc29.putReference( charIDToTypeID('null'), ref23 ); 

    executeAction( stringIDToTypeID('selectAllLayers'), desc29, 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; 

}; 

Hope this helps!

Davide

natrev
Legend
August 25, 2017

Hi Saikiran,

Hope this will helps...

if(documents.length>0) main(); 

function  main(){ 

    var docRef = app.activeDocument;   

    var myLayers=docRef.layers;

    for(i=0;i<myLayers.length;i++){

        if(myLayers.name.indexOf("Layer")!=-1 || myLayers.name.indexOf("Exposure")!=-1 || myLayers.name.indexOf("Levels")!=-1){

            docRef.activeLayer=myLayers;

            colorLayer (i, "red");

        }

        else{

            docRef.activeLayer=myLayers;

            colorLayer (i, "none");

        }

    }

}

function colorLayer (layerIndex, color){   

    var desc1 = new ActionDescriptor();   

    var ref1 = new ActionReference();   

    ref1.putIndex (stringIDToTypeID ("layer"), layerIndex);   

    desc1.putReference (stringIDToTypeID ("target"), ref1);   

    var desc2 = new ActionDescriptor();   

    desc2.putEnumerated (stringIDToTypeID ("color"), stringIDToTypeID ("color"), stringIDToTypeID (color));   

    desc1.putObject (stringIDToTypeID ("to"), stringIDToTypeID ("layer"), desc2); 

    executeAction (stringIDToTypeID ("set"), desc1, DialogModes.NO); 

-yajiv

Known Participant
August 25, 2017

Hi natrev,

Thank you so much for the script.It's working when I open a new document and apply the scripts.But I am getting this error if I open an existing comp and add exposure adjustment or put those layers in a group.Also, it's turning on all layers at the end of the script.

natrev
Legend
August 25, 2017

Hi

Could post Layer structure screen shot?

or Try this... code

if(documents.length>0) main(); 

function  main(){ 

    var docRef = app.activeDocument;   

    var myLayers=docRef.layers;

    for(i=0;i<myLayers.length;i++){

        var layVisible=myLayers.visible;

        docRef.activeLayer=myLayers;

        if(myLayers.name.indexOf("Layer")!=-1 || myLayers.name.indexOf("Exposure")!=-1 || myLayers.name.indexOf("Levels")!=-1){ colorLayer (i, "red"); }

        else { colorLayer (i, "none"); }

        myLayers.visible=layVisible;

    }

}

function colorLayer (layerIndex, color){   

    var desc1 = new ActionDescriptor();   

    var ref1 = new ActionReference();   

    ref1.putIndex (stringIDToTypeID ("layer"), layerIndex);   

    desc1.putReference (stringIDToTypeID ("target"), ref1);   

    var desc2 = new ActionDescriptor();   

    desc2.putEnumerated (stringIDToTypeID ("color"), stringIDToTypeID ("color"), stringIDToTypeID (color));   

    desc1.putObject (stringIDToTypeID ("to"), stringIDToTypeID ("layer"), desc2); 

    executeAction (stringIDToTypeID ("set"), desc1, DialogModes.NO); 

-yajiv