How to check if image is 8 bit or 16 bit and do some conditions?

I would like to check if currently active document have 8 or 16 bit and then do some conditions:


if (depth == 8bit) { do something}
if (depth == 16bit) {do something else }
else {do the same as for 8 bit}

Could you help me finding solution?

Thanks!

Here you go:

const getDepth = () => batchPlay(
[
   {
      _obj: "get",
      _target: [
         {
            _property: "depth"
         },
         {
            _ref: "document",
            _enum: "ordinal",
            _value: "targetEnum"
         }
      ],
   }
],{
   synchronousExecution: true
})[0].depth;

if(getDepth() === 16) {
   // logic for 16 bit
} else {
   // logic for 8 bit or other
}
2 Likes

Amazing, thank you Simon!

Hi Simon,
your solution makes total sense, but just out of curiosity: why do you prefer/use batchPlay and not DOM code for this? Is it because of speed, personal preference or anything else? Thanks!

Speed is usually a factor (batchplay is faster), however for a getter the difference wouldn’t really be noticable. I just build all my plugins with batchPlay only, because I know 100% of what’s scriptable is available through batchPlay, and only a subset of it via DOM - and I don’t really like mixing technologies.

That’s why I don’t really bother checking what’s available in the DOM, when I know I already have a solution at hand :slight_smile:

2 Likes

Thanks Simon, that makes sense! :slight_smile:

In addition to that, a lot of the verbosity of batchPlay can be abtracted over time by moving things to reusable functions. So effectively, I would probably only have to write something like

if(getLayerProperty("depth") === 16) {
   // logic for 16 bit
} else {
   // logic for 8 bit or other
}

which would be equally few lines of code as a DOM implementation.

4 Likes