Programatically mimic the behavior of the magic wand tool with "contiguous" off and tolerance 0? In other words, select all pixels in the image that are a given color?

I need to programatically select all pixels of a given color and then run Layer via Copy on the selection. In PS I can do this either with the magic wand tool or with Select > Color Range > Sampled Colors with Fuzziness: 0.

What does Alchemist say when you do a color range select?

const batchPlay = require("photoshop").action.batchPlay;

const result = await batchPlay(
[
   {
      _obj: "colorRange",
      fuzziness: 0,
      minimum: {
         _obj: "labColor",
         luminance: 76.52,
         a: -19.98,
         b: -21.7
      },
      maximum: {
         _obj: "labColor",
         luminance: 76.52,
         a: -19.98,
         b: -21.7
      },
      colorModel: 0,
      _options: {
         dialogOptions: "dontDisplay"
      }
   }
],{
   synchronousExecution: false,
   modalBehavior: "fail"
});

I’m guessing this means I can do it using batch play?

I think yes. Did you try running this in console?

Running it in the console fails with Event: colorRange may modify the state of Photoshop. Such events are only allowed from inside a modal scope. I’m not sure how to get around that in the console because I thought functions run in the console were already in a modal scope.

Instead, I tested it in my plugin and was able to run it in modal scope, but nothing happens on my document (i.e. no pixels are selected) and I am having trouble debugging it because I can’t seem to step into the batchPlay function. I can stop at my breakpoint at the line const batchPlay = require("photoshop").action.batchPlay; but from there, the step into and step commands in the debugger simply jump over the entire contents of the batchPlay function as if it wasn’t running.

You need to run it with executeAsModal(). This is required for any command that alters Ps state


Did you check the BP result? Could it be, that you don’t have that color pixels in your doc maybe?