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
2 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: