Skip to main content
Participating Frequently
December 2, 2013
Answered

How to check the layer's color?

  • December 2, 2013
  • 2 replies
  • 1159 views

I want to check the layer's color in the photoshop script (javascript). I'm writing a file exporter and I want to do one thing or another depending on the layer's color.

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

// 2013, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var ref = new ActionReference();

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

var layerDesc = executeActionGet(ref);

alert (typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color"))));

};

2 replies

c.pfaffenbichler
Community Expert
Community Expert
December 2, 2013

This should give you a list of the active Layer’s keys and their values.

Maybe this can help make things clearer.

#target photoshop

if (app.documents.length > 0) {

var ref = new ActionReference();

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

var layerDesc = executeActionGet(ref);

checkDesc2(layerDesc);

};

////// 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.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.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;

};

};

By switching in

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

you can similarily get the Document’s keys.

Participating Frequently
December 2, 2013
Dang, not sure how the space in »col or« was introduced, as I pasted the text a simple typo seems not that highly likely.

Could the line have been too long and got wrapped to another line? I don't know.

But with the same version of Photoshop the "listened to" code should work?

c.pfaffenbichler
Community Expert
Community Expert
December 3, 2013

But with the same version of Photoshop the "listened to" code should work?

Yes.

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
December 2, 2013

// 2013, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var ref = new ActionReference();

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

var layerDesc = executeActionGet(ref);

alert (typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color"))));

};

Participating Frequently
December 2, 2013

Thank you but I failed to mention that I'm using the CS3 version. You script doesn't work here - ExtendScript Toolkit 2 says that "this functionality may not be available in this version of Photoshop".

Also:

1) Why at my own risk? What's wrong with it?
2) I can't find any documentation or other information describing "Lyr ", "Ordn" and so on... Where can I get any reference?

Edit:

I've just found this: http://stackoverflow.com/questions/13893844/charidtotypeid-photoshop-javascript

Is this the answer to my both questions? Also - can it be that the commands you used were "listened to" in a different version of Photoshop and that's why they're not working on mine?

Finally, can I assume that these undocumented command will always work on the same version of Photoshop (on other machines, too)?

c.pfaffenbichler
Community Expert
Community Expert
December 2, 2013

1) Because I make no guarantees for Scripts.

2) Action Manager code (as for example recorded by ScriptingListener.plugin) was in all probability in use in CS3 but it’s possible that the key »color« was added to Layers later.

You are probably familiar with Document Object Manager (DOM) code and it is more easily readable, but Action Manager code is more »versatile«; check out the chapter »Action Manager« in »Photoshop CS4 Scripting Guide.pdf« (or rather the corresponding document for CS3) for an introduction.