Copy link to clipboard
Copied
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.
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"));
//
Copy link to clipboard
Copied
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 );
};
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I have tried changing red to green within the code, unfortunately it just hides all layers w/o leaving green on, do I need to change something else?
Copy link to clipboard
Copied
I am afraid you stumbled on a »typo«, »green« is named »grain«.
Try changing the line to
if (typeIDToStringID(theColor) == "grain") {theLayers.push([theName, theID])}
Copy link to clipboard
Copied
That actually worked, but that is bloody bananas! Why in the love of heaven there is a typo there? I do not get it! Thanks!
I just need a scrip that shows all layers, also maybe figure out how to do the other colous just in case I need more control
Copy link to clipboard
Copied
Well, typos happen …
A warning: If a Layer of a certain label color is situated in a hidden Group the Script would not result in the Group showing!
I just need a scrip that shows all layers
In that case collecting Arrays of Layer Indices would not be necessary and you could just use the function showLayer in the first for-clause.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I modified the script to do a $.writeln() comment for each layer. I coded the layers with each color, so here is what you should expect. The grain is odd, but yellow is different also.
Copy link to clipboard
Copied
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;
};
};
Copy link to clipboard
Copied
A warning: If a Layer of a certain label color is situated in a hidden Group the Script would not result in the Group showing!
If that might be an issue the easiest solution would be simply showing all Groups.
I just need a scrip that shows all layers
Does this help?
// 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"));
var hasBackground = applicationDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));
var theStart = 0;
if (hasBackground == false) {theStart++};
// show all layers;
var idShw = charIDToTypeID( "Shw " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list4 = new ActionList();
var ref4 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " )
for (var x = theStart; x <= theNumber; x++) {
ref4.putIndex( idLyr, x );
};
list4.putReference( ref4 );
desc5.putList( idnull, list4 );
executeAction( idShw, desc5, DialogModes.NO );
};
Copy link to clipboard
Copied
how do you find out the actual names of the other colours? just tried out grey, but it is not working either...