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 31, 2016

how can I find out the typos? or what I am suposed to write for yellow or Grey? blue and orange works fine, haha, this is an interesting interesting problem


how can I find out the typos?

Well … to be almost frank Scripting documentation and implementation for/in Photoshop (and other Adobe applications) could be said to leave something to be desired.

Chuck Uebele has already pointed out how to solve this particular issue (determining the color label names), for determining in Action Manager code other properties/settings of either the Application, a Document, a Layer, … I use the following code but if you are new to Photoshop Scripting it might be more convenient for you to try and stick with DOM code for a while.

// based on code by michael l hale;

// 2016, use it at your own risk;

#target photoshop

var ref = new ActionReference();

// apllication;

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

// document;

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

// layer;

// ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var theDesc = executeActionGet(ref);

checkDesc2(theDesc);

////////////////////////////////////

////// based on code by michael l hale //////

function checkDesc2 (theDesc) {

var c = theDesc.count;

var str = '';

for(var i=0;i<c;i++){ //enumerate descriptor's keys

  str = str + 'Key '+i+' = '+typeIDToStringID(theDesc.getKey(i))+': '+theDesc.getType(theDesc.getKey(i))+'\n'+getValues (theDesc, i)+'\n';

  };

alert("desc\n\n"+str);

};

////// check //////

function getValues (theDesc, theNumber) {

switch (theDesc.getType(theDesc.getKey(theNumber))) {

case DescValueType.ALIASTYPE:

return theDesc.getPath(theDesc.getKey(theNumber));

break;

case DescValueType.BOOLEANTYPE:

return theDesc.getBoolean(theDesc.getKey(theNumber));

break;

case DescValueType.CLASSTYPE:

return theDesc.getClass(theDesc.getKey(theNumber));

break;

case DescValueType.DOUBLETYPE:

return theDesc.getDouble(theDesc.getKey(theNumber));

break;

case DescValueType.ENUMERATEDTYPE:

return (typeIDToStringID(theDesc.getEnumerationValue(theDesc.getKey(theNumber)))+"_"+typeIDToStringID(theDesc.getEnumerationType(theDesc.getKey(theNumber))));

break;

case DescValueType.INTEGERTYPE:

return theDesc.getInteger(theDesc.getKey(theNumber));

break;

case DescValueType.LISTTYPE:

return theDesc.getList(theDesc.getKey(theNumber));

break;

case DescValueType.OBJECTTYPE:

return (theDesc.getObjectValue(theDesc.getKey(theNumber))+"_"+typeIDToStringID(theDesc.getObjectType(theDesc.getKey(theNumber))));

break;

case DescValueType.RAWTYPE:

return theDesc.getReference(theDesc.getData(theNumber));

break;

case DescValueType.REFERENCETYPE:

return theDesc.getReference(theDesc.getKey(theNumber));

break;

case DescValueType.STRINGTYPE:

return theDesc.getString(theDesc.getKey(theNumber));

break;

case DescValueType.UNITDOUBLE:

return (theDesc.getUnitDoubleValue(theDesc.getKey(theNumber))+"_"+typeIDToStringID(theDesc.getUnitDoubleType(theDesc.getKey(theNumber))));

break;

default:

break;

};

};