• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Apply layer color id based on name

Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Aug 25, 2017 Aug 25, 2017

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( "

...

Votes

Translate

Translate
Adobe
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Sure, if I have them in a group like this it's not working.BTW group name will change depending on the project

layer "test" turning on when I run the script.

Here is a basic structure and I added level and exposure to the file and I saved it out and then I opened it and ran the script and got the error

Once again thank you for helping me

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Hi,

the following should work also for layers contained within Groups:

// Based on some Paul Riggott's code

#target photoshop

app.bringToFront();

app.activeDocument.suspendHistory('Coloring some Layers Red', '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;

};

Hope this helps,

Davide

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Amazing!!! Thank you so much.This works perfectly.One more request, please.I would like to run the other script that removes red id but gives an error if one of the layer has exposure, layer or level to it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

So you'd like to have two scripts, one that applies red to those layers, and another which resets red, do I get it right?

Davide

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Yes you are correct!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Hi,

this one sets your layers Red:

#target photoshop

app.bringToFront();

app.activeDocument.suspendHistory('Coloring layers Red', '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;

};

this one resets your layers to color "None":

#target photoshop

app.bringToFront();

app.activeDocument.suspendHistory('Resetting layers color', '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"), charIDToTypeID("None"));     

        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;

};

in case this answers your original question, please mark it as Correct.

Cheers,

Davide

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Hi Davide,

The reset is removing the id color in the file even if it has exposure and layer to it.Is it possible to remain the color as it is to those layers that have those names until they are changed?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

I did get that this was the intended behavior: one script sets the Red for Layers, Levels and Exposure strings, the other script reverts the Red (i.e. removes the Red to those layers and brings them to no color). If that's not the case, I'm not getting what the second script should do – let me know! 🙂

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Sorry, it still has exposure and level to it but named differently like may be.shirt exp and sleeve level.If they are still named as exposure and level then the id color remains the same

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

So basically you'd like to have a  script that sets Red for whatever contains those "layers" "levels" and "exposure" strings, and at the same times resets the color to None to those layers that don't contain the strings, right? No need to split this in two scripts...

Did I get it?

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

I think the first script actually does that.I still would like to have the reset as the other script as I want to change the name of my layers and run the script just in case if I missed any layers that I need to rename.

Something like this

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

We're getting there... 🙂

So the first script *just* sets the 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;

};

The 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( "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;

};

Cheers,

Davide

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

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

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

LATEST

Thank you! Thank you! Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines