8 Replies Latest reply on Dec 2, 2013 11:07 PM by c.pfaffenbichler

    How to check the layer's color?

    the.real.nps

      color.png

      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.

        • 1. Re: How to check the layer's color?
          c.pfaffenbichler Level 9

          // 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. Re: How to check the layer's color?
            the.real.nps Level 1

            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)?

            • 3. Re: How to check the layer's color?
              c.pfaffenbichler Level 9

              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.

              • 4. Re: How to check the layer's color?
                the.real.nps Level 1

                I've already started using the script listener and its possibilities seem great. But can you tell me (if you know) whether I can assume that the code "listened to" will always work (on other machines/with other versions of photoshop)?

                 

                Edit:

                Your script had "col or" instead of "color" (and the other codes were cryptic enough that I didn't even think this one would be a sensible word). So when I fixed it it worked like a charm! Thx a lot.

                 

                But I still want that answer. From you or anyone else.

                 

                Edit2:

                Actually - any chance for comments to your script? I'd like to incorporate it in my own script and need to know what exactly it does.

                • 5. Re: How to check the layer's color?
                  c.pfaffenbichler Level 9

                  I've already started using the script listener and its possibilities seem great. But can you tell me (if you know) whether I can assume that the code "listened to" will always work (on other machines/with other versions of photoshop)?

                  No, there can at least be backward compatibility issues, possibly even forward ones …

                   

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

                   

                  need to know what exactly it does.

                  The Script gets the layer’s Action Descriptor and the Enumeration Value of the color Key.

                  • 6. Re: How to check the layer's color?
                    c.pfaffenbichler Level 9

                    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)))+"_"+typeIDToStr ingID(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.getObject Type(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.getUn itDoubleType(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.

                    • 7. Re: How to check the layer's color?
                      the.real.nps Level 1
                      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?

                      • 8. Re: How to check the layer's color?
                        c.pfaffenbichler Level 9

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

                        Yes.