How to set clipping mask?

I could find out from this document whether the layer is used as a clipping mask or not.

The document also states that the “is cripping mask” property is not read-only, but can also be written.
However, the following code did not work.

I tried this method instead, but it didn’t work either.

What should I do?

Thank You!

function test(){
  core.executeAsModal(async () => {
    try {
      const activeLayers = app.activeDocument.activeLayers;
      await activeLayers.forEach(layer => layer.selected = false);
      for(const layer of activeLayers){
        layer.selected = true;
        console.log(layer);
        console.log(layer.isClippingMask);
        layer.isClippingMask = true;
        layer.name = "test";
        console.log(layer.isClippingMask);
        layer.selected = false;
      }
      console.log("test");
    } catch (e) {
      console.log(e);
    }
  });
}

What do your logs say?

No error, but no change either…

The name change is put in to make sure the process is working.
This one is working.

image

Could you remove the renaming line and have the log right after clipping mask change?
Also, is this layer visible? I remember there was a case when you couldn’t change properties for invisible layers

I changed the code as follows and the result of the run is shown in the figure.

function test(){
  core.executeAsModal(async () => {
    try {
      const activeLayers = app.activeDocument.activeLayers;
      await activeLayers.forEach(layer => layer.selected = false);
      for(const layer of activeLayers){
        layer.selected = true;
        layer.isClippingMask = true;
        console.log(layer.isClippingMask);
        layer.selected = false;
      }
      console.log("test");
    } catch (e) {
      console.log(e);
    }
  });
}

image

Incidentally, the use of BatchPlay allowed me to perform the behavior I was looking for.

async function setClippingLayer(layerid){
  let command;
  let result;
  console.log(layerid);
  command = {"_obj":"groupEvent","_target":[{"_ref":"layer","_id":layerid}]};
  result = await action.batchPlay([command], {});
}


function test(){
  core.executeAsModal(async () => {
    try {
      const activeLayers = app.activeDocument.activeLayers;
      await activeLayers.forEach(layer => layer.selected = false);
      for(const layer of activeLayers){
        setClippingLayer(layer.id);
      }
      console.log("test");
    } catch (e) {
      console.log(e);
    }
  });
}

You didn’t answer about layer visibility. Is it visible when you try to change the clipping? What happens if you add layer.visible = true; before changing the mask? As I said, this used to be an issue. Maybe still is

Oh, I’m sorry…
I run the code with all layers with layer.visible = true;.
However, ClippingLayer has not been set to True.

1 Like