How to change layer opacity by name inside a group?

Hello friends?
How to change the opacity of an adjustment layer by its name inside a group without selecting it?
The aim is not to expand the group layer stack, as there will be at least 8 adjustment layers grouped together.

1 Like

you can loop through layers the normal way and check the .kind of each layer… if it’s a group then loop through its .layers cheacking the .name property… once you find the name you’re looking for you set the opacity of that specific layer.

this will not select layers and won’t expand groups AFAIK

2 Likes

just got to office, here’s a working code:

const app = require("photoshop").app;

async function setOpacity() {
   let wantedOpacity = 30
   let wantedLayer = "my_Layer_name"
   await executeAsModal(async () => {
      let doc = app.activeDocument;
      doc.layers.forEach(layer =>{
         if (layer.kind == "group"){
            layer.layers.forEach(gLayer => {
               //now looping through group layers : one level deeper
               if (gLayer.name == wantedLayer){
                  //layer found
                  // you can also check its kind if you like to
                  gLayer.opacity = wantedOpacity
               } // if wanted layer found :: END
            }) // group layers loop :: END
         }  // if kind is "group" :: END
      }) // doc layers loop :: END

   }, { "commandName": "Setting Opacity..." }) //executeAsModal :: END
}
1 Like

Hi @Maher , thanks for the beautiful explanation and for sharing your knowledge.

1 Like

glad to help
please note that this a proof of concept code and could be done more elegantly (not so proud of my habit of nesting code :sweat_smile:)

wish you the best with your project!

1 Like

Assuming a unique name, I would try
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/layers/#getbyname

2 Likes

this is a great solution for unique names.
if not then it’ll return the first layer (lowest index) matching the name.

also theres not guarantee the returned layer is inside a group.

2 Likes

Also, you can limit your search to the group if you know it already. You can use theGroup.layers.getByName().

3 Likes