# API Extension - batchPlay and artboardEnabled

**URL:** <https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671>\
**Category:** Photoshop\
**Tags:** javascript\
**Created:** [March 20, 2021, 12:54am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671 "2021-03-20T00:54:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 12:54am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/1 "2021-03-20T00:54:33Z")

</div>

Hiya!  
I’m trying to get my head around batchPlay as someone who used ExtendScript extensively (lol), but didn’t use ActionDescriptors much at all.  
I’ve written the below as a starting point for a “get artboards” API extension. I’m currently targeting a single layer, but my end goal is an array of artboard objects.  
When run, the Promise is rejected with an error code of 2000, and the value “Error: NAPI API failure: String expected”.  
I’ll be completely honest, I haven’t the foggiest of how to proceed from here!  
I’ve done the obvious and appended the batchPlay function with .toString(), but all I get there is “[object Promise]” as my returned value. I guess I’m not actually getting the artboardEnabled boolean value and am in fact getting some kind of object back from isArtboard()?  
Is this even the best way of doing this?

```
const app = window.require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;

require('photoshop').app.Layer.prototype.isArtboard = async () => {
return await batchPlay (
    [{
        "_obj": "get",
        "_target": [{
                "_property": "artboardEnabled"
            },
            {
                "_enum": "ordinal",
                "_value": "targetEnum"
            }
        ],
        "_options": {
            "dialogOptions": "dontDisplay"
        }
    }], {
        "synchronousExecution": false,
        "modalBehavior": "fail"
    });
}

require('photoshop').app.activeDocument.layers[1].isArtboard();
```

---

<div class="post-metadata">

**Author:** ![Pierre\_G](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pierre_g/32/971_2.png) [@Pierre\_G](https://forums.creativeclouddeveloper.com/u/Pierre_G)\
**Post date:** [March 20, 2021, 1:27am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/2 "2021-03-20T01:27:06Z")

</div>

Hey! You might want to try this

```
function getArtboardEnabled() {
    const batchPlay = require("photoshop").action.batchPlay;

    return batchPlay(
        [{
            "_obj": "get",
            "_target": [{
                    "_property": "artboardEnabled"
                },
                {
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                },
                {
                    "_ref": "document",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }
            ]
        }], {
            "synchronousExecution": true
        })[0].artboardEnabled
}
```

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 10:27am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/3 "2021-03-20T10:27:58Z")

</div>

Hi Pierre,  
thanks, this is perfect!  
I understand it except for the penultimate line; what’s the` [0].artboardEnabled` for after the batchPlay JSON? Like, I get what it is, an array index and an object, that I guess is delivering the “payload” defined in the JSON? I’m also guessing that the [0] index is a reference to the activeDocument? Does batchPlay return an array, which in turn is returned by the function?

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 10:53am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/4 "2021-03-20T10:53:19Z")

</div>

OK , with further testing I can see now that batchPlay returns [object object] and the [0].artboardEnabled is returning the value, in theory.  
I’m getting inconsistent results - it will return the same result for all layers regardless of their actual artboardEnabled value. I get true for one document and false for another, I can’t work out what’s defining that value and why it isn’t changing:

```
const app = window.require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const doc = app.activeDocument;

function getArtboardEnabled() {
    const batchPlay = require("photoshop").action.batchPlay;

    return batchPlay(
        [{
            "_obj": "get",
            "_target": [{
                    "_property": "artboardEnabled"
                },
                {
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                },
                {
                    "_ref": "document",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }
            ]
        }], {
            "synchronousExecution": true
        })[0].artboardEnabled
}

for(i=0;i<doc.layers.length;i++){
    console.log(doc.layers[i].name + " " + getArtboardEnabled(doc.layers[i]));
}
```

---

<div class="post-metadata">

**Author:** ![Pierre\_G](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pierre_g/32/971_2.png) [@Pierre\_G](https://forums.creativeclouddeveloper.com/u/Pierre_G)\
**Post date:** [March 20, 2021, 10:53am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/5 "2021-03-20T10:53:34Z")

</div>

See in front of batchPlay, there’s the return instruction. It’s equivalent to:

```
function getArtboardEnabled() {
    const batchPlay = require("photoshop").action.batchPlay;

    var foo = batchPlay(
        [{
            "_obj": "get",
            "_target": [{
                    "_property": "artboardEnabled"
                },
                {
                    "_ref": "layer",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                },
                {
                    "_ref": "document",
                    "_enum": "ordinal",
                    "_value": "targetEnum"
                }
            ]
        }], {
            "synchronousExecution": true
        });

    var bar = foo[0].artboardEnabled
    console.log(bar)
}
```

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [March 20, 2021, 10:54am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/6 "2021-03-20T10:54:32Z")

</div>

By default, batchPlay returns `Promise<Object[]>`, unless you add `synchronousExecution:true` to the options, then it’s just an Object array.  
The index isn’t related to the activeDocument, the function just returns multiple descriptors (objects) since you can also feed it multiple ones.

In case of a getter, this doesn’t make too much sense, but if you’d do something like `batchPlay([*makeLayer*, *moveLayer*],{})`, it would return a promise including both of the “processed” descriptors.

For example the makeLayer descriptor might look like

```
  {
      "_obj": "make",
      "_target": [
         {
            "_ref": "layer"
         }
      ]
   }

```

and will be returned as

```
  {
      "_obj": "make",
      "_target": [
         {
            "_ref": "layer"
         }
      ],
      "layerID": 3,
   }

```

including additional information such as the ID that got assigned by PS.

So if you’re specifically targeting a property via a getter, you always have to call `[0].property` on the result.

---

<div class="post-metadata">

**Author:** ![Pierre\_G](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pierre_g/32/971_2.png) [@Pierre\_G](https://forums.creativeclouddeveloper.com/u/Pierre_G)\
**Post date:** [March 20, 2021, 10:56am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/7 "2021-03-20T10:56:17Z")

</div>

Please note that this code will work on the _current_ layer of the _current_ document!

If you need to taget a specific layer, then you have to replace

```
{
    "_ref": "layer",
    "_enum": "ordinal",
    "_value": "targetEnum"
}

```

by

```
{
    "_ref": "layer",
    "_id": myLayer_ID // number
}
```

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 11:05am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/8 "2021-03-20T11:05:15Z")

</div>

ah, ok!  
So what do I do in the instance of this:

```
for(i=0;i<doc.layers.length;i++){
    if(getArtboardEnabled(doc.layers[i])) {
        arr.push(doc.layers[i])
    }
}

```

Do I need to get the layer ID and pass it as a variable in the JSON? Or leave the JSON as ordinal/targetEnum and instead set `doc.activeLayer = doc.layers[i]` in my loop?  
Forgive me if I’m asking stupid questions, I’m finding this batchPlay/promises/async stuff quite a steep learning curve!

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [March 20, 2021, 11:49am UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/9 "2021-03-20T11:49:41Z")

</div>

> [@Timothy\_Bennett](#):
>
> Do I need to get the layer ID and pass it as a variable in the JSON?

You don’t need to, but in my opinion that’s the best approach. As you’ve said, you can also set the active layer and then get the desired property, but that’s just one extra Photoshop operation, and those are usually slower so I’d try to minimize them.

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 12:36pm UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/10 "2021-03-20T12:36:00Z")

</div>

Banging, I’m not so dumb after all 😀  
Thanks Simon & Pierre!  
Here’s where I ended up should anyone else want to return an array of document artboards:

```
const app = window.require("photoshop").app;
const doc = app.activeDocument;

var arr = [];

for(i=0;i<doc.layers.length;i++){
    if(getArtboardEnabled(doc.layers[i])) {
        arr.push(doc.layers[i])
    }
}

function getArtboardEnabled(obj) {
    const batchPlay = require("photoshop").action.batchPlay;    
    return batchPlay(
        [{
            "_obj": "get",
            "_target": [{
                    "_property": "artboardEnabled"
                },
                {
                    "_ref": "layer",
                    "_id": obj._id
                }
            ]
        }], {
            "synchronousExecution": true
        })[0].artboardEnabled
}
```

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 20, 2021, 4:07pm UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/11 "2021-03-20T16:07:51Z")

</div>

I sat down and worked on this for a while, and I started to feel like it had all clicked and I was really getting it. What hubris!  
The code below all works up until the `isLinked()` function. I get the following error:  
`Script snippet %2312:99 Uncaught TypeError: Cannot read property 'smartObject' of undefined`  
Now, I know that the Smart Object is being passed to `isLinked()` successfully, so I’m guessing that it’s a pending promise that’s causing it to be undefined?

```
const app = window.require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const doc = app.activeDocument;
const artboards = [];
const linkedSmartObjects = [];
const embeddedSmartObjects = [];

getArtboards();

for (i = 0; i < artboards.length; i++) {
  getSmartObjects(artboards[i]);
}

function getArtboards() {
  for (i = 0; i < doc.layers.length; i++) {
    if (getArtboardEnabled(doc.layers[i])) {
      artboards.push(doc.layers[i])
    }
  }

  function getArtboardEnabled(artboard) {
    return batchPlay(
      [{
        "_obj": "get",
        "_target": [{
            "_property": "artboardEnabled"
          },
          {
            "_ref": "layer",
            "_id": artboard._id
          }
        ]
      }], {
        "synchronousExecution": true
      })[0].artboardEnabled
  }
};

function getSmartObjects(artboard) {
  for (i = 0; i < artboard.children.length; i++) {
    if (getLayerKind(artboard.children[i]) === 5) {
      if (isLinked(artboard.children[i])) {
        linkedSmartObjects.push(artboard.children[i])
      } else {
        embeddedSmartObjects.push(artboard.children[i])
      }
    }
  }

  function getLayerKind(layer) {
    return batchPlay(
      [{
        "_obj": "get",
        "_target": [{
            "_property": "layerKind"
          },
          {
            "_ref": "layer",
            "_id": layer._id
          }
        ],
        "_options": {
          "dialogOptions": "dontDisplay"
        }
      }], {
        "synchronousExecution": true,
        "modalBehavior": "fail"
      })[0].layerKind;
  }
  function isLinked(smartObject) {
    return batchPlay(
      [
         {
            "_obj": "get",
            "_target": [
               {
                  "_property": "smartObject"
               },
               {
                  "_ref": "layer",
                  "_id": smartObject._id
               }
            ],
            "_options": {
               "dialogOptions": "dontDisplay"
            }
         }
      ],{
         "synchronousExecution": false,
         "modalBehavior": "fail"
      })[0].smartObject.linked;
  };
};
```

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [March 21, 2021, 2:31pm UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/12 "2021-03-21T14:31:29Z")

</div>

> [@Timothy\_Bennett](#):
>
> `"synchronousExecution": false`

makes it async, so you’re trying to access the 0-index on an unresolved promise and not an array, which results in undefined.

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [March 21, 2021, 9:05pm UTC](https://forums.creativeclouddeveloper.com/t/api-extension-batchplay-and-artboardenabled/2671/13 "2021-03-21T21:05:42Z")

</div>

Oops! Move along, nothing to see here 😅

Thanks again, I feel like I’m sort of getting it!
