Color Picker Eyedropper

Hi,
The following script for the color picker works perfectly except, when the eyedropper picks a color in the document, the color is changed in the dialog panel but, clicking OK stop the script without setting the color.
The error is “Cannot read property ‘_value’ of undefined”.
Can someone show where I erred?

    const ColorPicker =  () => {
    const result =  batchPlay(
   [
   {
      "_obj": "showColorPicker",
			"context": "General Picker",
      "application": {
         "_class": "null"
      },
      "value": true,
      "color": {
         "_obj": "HSBColorClass",
         "hue": {
            "_unit": "angleUnit",
            "_value": 136.9390869140625
         },
         "saturation": 80.7843137254902,
         "brightness": 54.90196078431372
      },
      "RGBFloatColor": {
         "_obj": "RGBColor",
         "red": 26.902723610401154,
         "grain": 140.00000685453415,
         "blue": 58.832683861255646
      },
      "dontRecord": true,
      "forceNotify": true,
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
	 }
],{
   "synchronousExecution": true,
   "modalBehavior": "fail"
});
    const picked = result[0];


    pickedHue = result[0].color.hue._value;

    console.log("You picked:", picked.color.hue._value);
    return picked;
    }

Judging by the error, this is the line where it breaks. Moving from right to left you’ll see that the hue property that you’re trying to access _value from seems to be undefined. So up until this point, everything worked fine, meaning that
result
result[0]
and
result[0].color
are all defined.

I assume that the color returned by the picker is not of type HSBColorClass, so there’s no hue property. If you just return result[0].color and log that you’ll see whether it’s RGB, CMYK, HSB etc.

Thank for your prompt reply, sorry for not being precise.

OK first:
result[0].color is for HSB properties,
result[0].RGBFloatColor is for RGB colors.

console.log("pickedHSB = "+result[0].color.hue._value);
console.log("pickedHSB = "+result[0].color.saturation);
console.log("pickedHSB = "+result[0].color.brightness);
console.log("pickedRGB = "+result[0].RGBFloatColor.red);
console.log("pickedRGB = "+result[0].RGBFloatColor.grain);
console.log("pickedRGB = "+result[0].RGBFloatColor.blue);

Results when the color is picked from the dialog panel
./index.js:255 pickedHSB = 299.28955078125
./index.js:256 pickedHSB = 80.7843137254902
./index.js:257 pickedHSB = 54.90196078431372
./index.js:258 pickedRGB = 138.6614754796028
./index.js:259 pickedRGB = 26.902723610401154
./index.js:260 pickedRGB = 140.00000685453415

And this is the result when color is picked on the picture:
./index.js:255 Uncaught TypeError: Cannot read property ‘_value’ of undefined
at ColorPicker (VM49 index.js:255)
at test2 (VM49 index.js:8094)
at e.exports. (VM49 index.js:8121)
at uxp://uxp-internal/domjs_scripts.js:2
at Object.runWithNativeHandler ()
at I (uxp://uxp-internal/domjs_scripts.js:2)
at uxp://uxp-internal/domjs_scripts.js:2
at O (uxp://uxp-internal/domjs_scripts.js:2)
at k (uxp://uxp-internal/domjs_scripts.js:2)
at e (uxp://uxp-internal/domjs_scripts.js:2)

Does this help?

not really :thinking: So you have two different places from which you call the batchPlay function, one works and the other doesn’t?
I still don’t see why you don’t just log result[0].color in the case where you’re getting the error to see what’s actually inside there.

PS: If you add code to a post, please use the code highlighting feature of the editor, that makes it 10x easier to read :slight_smile: (especially if you add proper indents instead of having the code pushed all to the left)

To answer your question
console.log("pickedHSB = "+result[0].color); ===>>> pickedHSB = [object Object]
Which means you have nothing unless you add what is missing.
Since I am not a pro programmer I didn’t think it important. Sorry!

No worries! Just a tip to make it more readable in future posts.

Regarding the log: Try to just log the object itself, without any string concatenation. It will show you the actual object then. Or you can separate multiple values per comma, that should also work:

image

Thank you very much. You taught me something.
Without concatenation the result is: the eyedropper gives RGB colors and undefined HSB colors.
Since I want the hue I think I will try the script without the RGBFloatColor.
If it doesn’t work I’ll have to convert :hot_face: RGB2HSB.
Thanks again, found the bug to be repaired.