Layer fill how does it work

I will fill a new created layer with a color, e.g. black. I use the code below: The code stops at the last row, and I have tried other versions of the .fill. I have search internet in two weeks and can not find a solution. Can I get help here?
/Anders


async function setColor() {
    const SC = require("photoshop").app.SolidColor;
    const bgcolor = new SC;
    bgcolor.rgb.hexValue="ffffff";
    console.log(`Färg: ${bgcolor.SolidColor} och bgc= ${bgc}`);
    return bgcolor.rgb.hexValue;
}

async function createNeededLayers(nDoc) {
      const al = nDoc.activeLayers[0];
      const bgLayer = await nDoc.duplicateLayers([al]);
      var newBL = await nDoc.createLayer();
console.log('createNeededLayers');
      console.log(`Lager= ${bgLayer.id}, aktivt lager är: ${al.id} och nytt lager är ${newBL.id}`) ;
      newBL.opacity=100;
      newBL.sendToBack();

      activeDocument.layers[newBL.id].fill(setColor());
}

I would suggest wrapping the suspicious parts of your code in a try/catch block so you can catch any exception that may occur and get some helpful information from it. In doing so it might be worth to separate out your call to ‘setColor’ into its own statement rather than doing a one-liner (might make debugging a bit easier) on the last row.

P.S. This expression in ‘setColor’ looks a bit suspicious to me: ${bgcolor.SolidColor}.

I’m not entirely sure, but app.activeDocument.layers creates an array of layers in the active document, and you can call them with sequential numbers layers[0], layers[1], and so on.

I looks like you are trying to chose a layer based on the layer’s id [newBL.id], which likely is a number that does not match the sequential number of the layer in the “layers” array. Photoshop assigns and “id” to the layer when it’s created, and it’s not a sequential number.

I know you can use batchPlay to select a layer based on a layer id. There might be a way to do it with DOM APIs also, but I’ve not looked for this.

There is no layer.fill() method yet. You might need to use batchPlay for now.

Thanks, that make the work.

Thanks for the observertion.