How to check if layer with specific name exists?

Hi,

How to check in UXP if layer with specific name exists?

Scenario:

If layer exists do something, if not, do something else.

Thank You!

A bit modified answer of this topic

import {app} from "photoshop"

const layerExistsByName = (name) => {
  return Boolean(app.activeDocument?.layers?.some(layer => layer.name === name))
}

const exists = layerExistsByName("Some layer name")
console.log(exists) // true | false
3 Likes

Thank You so much! That’s super smart. I’ll test this shortly and I’ll let you know how it works.

Cheers!

note: for thousands of layers this can be magnitude slower than necessary. You can use batchPlay with name in reference to getting the fastest performance. If you need performance.

1 Like

Hi Karmalakas,

It works perfectly, thank you so much! the only change I had to make was:

const layerExistsByName = (name) => {

Cheers!

1 Like

Oh, mistype :slight_smile: Fixed :wink:

Note: I think accepted solution would not work anymore on API v2 (or is it manifest v5…?), as doc.layers will return only root level layers. So if the name you search for is in some group, it wouldn’t find it. @Jarda’s proposed option with BatchPlay probably would be the best bet :thinking: I believe function would be something like:

import {action} from "photoshop"

const layerExistsByName = (name) => {
  const layers = action.batchPlay(
    [{
        _obj: 'get',
        _target: [
            {_ref: 'layer', _name: name},
            {_ref: 'document', _enum: 'ordinal', _value: 'targetEnum'}
        ]
    }],
    {
        synchronousExecution: true,
        modalBehavior: 'execute'
    }
  ) || null

  return Boolean(layers?.length > 0)
}

Sorry, didn’t test this in practice, so might be something I missed. Also not sure what’s the structure of response for this getter

There is something like “validateReference” function (I don’t remember where exactly). So no need to run batchPlay at all. That could be even faster.

It’s here in the docs, but I don’t understand how (or in which cases) to use it :confused:

1 Like

Seems to work like that:

1 Like

Just noticed there’s new batchPlaySync() with the only difference from batchPlay() being the return type. Probably that’s why typescript complained about synchronousExecution option (and never was in TS types, probably because that was the plan to have separate function all along).

A bit weird it throws an error instead of returning false. Based on the docs, it should return Boolean. Wonder in which case it does return false

Bit weird that it throws an error instead of returning false, but you could write a function like that:

const refExists = (ref) => {
  try {
    return require("photoshop").action.validateReference(ref);
  } catch(e) {
    return false
  }
}

edit: You were a split second quicker :smile:

1 Like

I laughed so loud :rofl: Word for word same concern

I was also surprised at how shockingly equal the wording was :sweat_smile:

Seems to work correctly for layer references though :smiley:

image

I’d still do the try/catch just to be on the safe side.

2 Likes

Does it validate only on active document?

Haven’t tried yet, but checking for properties doesn’t work:
image
But for such a use case you’d use batchPlay anyways, I guess.

Works also on other docs, but I already expected that since you can pass an array of references.

2 Likes