# Layer fill how does it work

**URL:** <https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301>\
**Category:** UXP Plugin API\
**Created:** [May 16, 2023, 7:00pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301 "2023-05-16T19:00:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndersBergquist](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@AndersBergquist](https://forums.creativeclouddeveloper.com/u/AndersBergquist)\
**Post date:** [May 16, 2023, 7:00pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/1 "2023-05-16T19:00:42Z")

</div>

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

```auto

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());
}

```

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [May 16, 2023, 7:42pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/2 "2023-05-16T19:42:11Z")

</div>

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}`.

---

<div class="post-metadata">

**Author:** ![AnthonyK](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AnthonyK](https://forums.creativeclouddeveloper.com/u/AnthonyK)\
**Post date:** [May 16, 2023, 9:19pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/3 "2023-05-16T21:19:53Z")

</div>

> [@AndersBergquist](#):
>
> `activeDocument.layers[newBL.id].fill(setColor());`

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.

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [May 16, 2023, 9:34pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/4 "2023-05-16T21:34:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndersBergquist](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@AndersBergquist](https://forums.creativeclouddeveloper.com/u/AndersBergquist)\
**Post date:** [May 17, 2023, 5:11pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/5 "2023-05-17T17:11:29Z")

</div>

Thanks, that make the work.

---

<div class="post-metadata">

**Author:** ![AndersBergquist](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@AndersBergquist](https://forums.creativeclouddeveloper.com/u/AndersBergquist)\
**Post date:** [May 17, 2023, 5:12pm UTC](https://forums.creativeclouddeveloper.com/t/layer-fill-how-does-it-work/6301/6 "2023-05-17T17:12:13Z")

</div>

Thanks for the observertion.
