Skip to main content
Participating Frequently
July 30, 2016
Answered

how to hide/show layers depending on layer colour?

  • July 30, 2016
  • 1 reply
  • 2149 views

Hello Photoshop geniuses,

I have used Photoshop for a good number of years but I have hardly ventured in doing scripting.

I need to come up with a script that toggles layer visibility depending on color (red/orange/yellow/green/blue/purple/grey)

I also need another script that cycles between layers and checks names (if FG or BG) and depending on what names they have, say, if layer is FG, then assign layer colour blue, otherwise, if layer is BG, then asign color RED

If you are questioning why I need this, I am a texture artist that needs to create to versions of the same texture, and the colours change but use the same mask in a folder.

Thanks for the help, would be greatly appreciated.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

I would recommend creating one thread for one issue.

//  show red layers, hide the other layers;

// 2016, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

var theOthers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

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

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var visible = layerDesc.getBoolean(stringIDToTypeID("visible"));

var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));

if (typeIDToStringID(theColor) == "red") {theLayers.push([theName, theID])}

else {theOthers.push([theName, theID])}

};

}

catch (e) {};

};

// show red layers;

for (var m = 0; m < theLayers.length; m++) {

showLayer (theLayers[1], false);

};

// hide others;

for (var n = 0; n < theOthers.length; n++) {

showLayer (theOthers[1], true);

};

};

////// show layer //////

function showLayer (theID, showOrHide) {

if (showOrHide == false) {var idHd = charIDToTypeID( "Shw " )}

else {var idHd = charIDToTypeID( "Hd  " )};

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var list1 = new ActionList();

            var ref1 = new ActionReference();

  ref1.putIdentifier(charIDToTypeID( "Lyr " ), theID);

        list1.putReference( ref1 );

    desc2.putList( idnull, list1 );

executeAction( idHd, desc2, DialogModes.NO );

};

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
July 30, 2016

I would recommend creating one thread for one issue.

//  show red layers, hide the other layers;

// 2016, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

var theOthers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

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

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var visible = layerDesc.getBoolean(stringIDToTypeID("visible"));

var theColor = layerDesc.getEnumerationValue(stringIDToTypeID("color"));

if (typeIDToStringID(theColor) == "red") {theLayers.push([theName, theID])}

else {theOthers.push([theName, theID])}

};

}

catch (e) {};

};

// show red layers;

for (var m = 0; m < theLayers.length; m++) {

showLayer (theLayers[1], false);

};

// hide others;

for (var n = 0; n < theOthers.length; n++) {

showLayer (theOthers[1], true);

};

};

////// show layer //////

function showLayer (theID, showOrHide) {

if (showOrHide == false) {var idHd = charIDToTypeID( "Shw " )}

else {var idHd = charIDToTypeID( "Hd  " )};

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var list1 = new ActionList();

            var ref1 = new ActionReference();

  ref1.putIdentifier(charIDToTypeID( "Lyr " ), theID);

        list1.putReference( ref1 );

    desc2.putList( idnull, list1 );

executeAction( idHd, desc2, DialogModes.NO );

};

Participating Frequently
July 30, 2016

That has worked wonderfully.

I'll see if I can create other JS from modifying this,

Would be amazing to have a JS to show all layers, just hide specific colours (like red) or just show specific colours, thank you very much, I'll see if I can compose this by my self.

c.pfaffenbichler
Community Expert
Community Expert
July 30, 2016

If you want to use functionality that eludes Photoshop’s Document Object Model (DOM) use ScriptingListener.plugin to record the Action Manager code for the operation and use that in the Script.

I think changing a Layer’s label color falls under that.