# Moving layers with BatchPlay?

**URL:** <https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039>\
**Category:** UXP Plugin API\
**Created:** [June 6, 2021, 8:40am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039 "2021-06-06T08:40:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [June 6, 2021, 8:40am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/1 "2021-06-06T08:40:38Z")

</div>

What would be the correct batchPlay syntax to move one layer above or below another?  
I know I can do this in javascript API, but I have a long chain of batchPlay descriptors that I don’t want to break. So I need a batchPlay oneliner to move one layer above or below another by name.

The task is even more difficult then I thought initially.  
Listing layers with activeDocument.layers and trying to use indexes in batchPlay

```auto
batchPlay([{
          _obj: "move",
          _target: [
            {
              _ref: "layer",
              _enum: "ordinal",
              _value: "targetEnum",
            },
          ],
          to: {
            _ref: "layer",
            _index: 2
          },
          adjustment: false,
            }
      ],{})

```

gives quite unpredicatable results, it looks like indexes used internally do not correspond to indexes in the layers, there are some hidden elements and groups that are not listed

---

<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:** [June 6, 2021, 9:25am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/2 "2021-06-06T09:25:19Z")

</div>

If you know the name of the layer before your batchPlay chain, you could simply do:

1. Read index by name
2. Subtract 1 if you want to put a layer below, or keep it as is to put it above
3. Move layer to the new index

You probably already figured this out, so step 1) might be causing the issues.  
I wouldn’t say indices are unpredictable, but they can be tricky for sure. Here are two things you need to consider:

- Groups in Photoshop occupy two indices: one “Layer Section Start” (visible in the layer stack) and one “Layer Section End” (invisible)
- Indices are affected by the document having a background layer or not. This can shift things around by 1

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [June 6, 2021, 9:37am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/3 "2021-06-06T09:37:14Z")

</div>

That’s basically what I have tried.  
Problem is that it looks like not only Groups occupy 2 indexes, if you clip parent layer to bottom layer with Cmd+Alt+G (which is also kind of grouping), indexes start behaving odd.

For example, I can see 18 layers in the list, some of which would be groups, but moving to 18th index moves to the wrong place.  
Keeping in mind that there can be many nested groups and many levels of clipped layers, calculation of the correct index to move to becomes very tricky. And if your chain of descriptors creates layers, I think it makes it impossible to calculate index beforehand because layers are not yet there when you create descriptors for batchPlay.

---

<div class="post-metadata">

**Author:** ![liuxiao](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/liuxiao/32/1905_2.png) [@liuxiao](https://forums.creativeclouddeveloper.com/u/liuxiao)\
**Post date:** [September 28, 2021, 12:48pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/4 "2021-09-28T12:48:29Z")

</div>

> [@simonhenke](#):
>
> 1. Read index by name

Hello Simon, can I get some suggestions with how to do this?

---

<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:** [September 28, 2021, 4:20pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/5 "2021-09-28T16:20:12Z")

</div>

Sure, layer descriptors have a property called `itemIndex`, that you can get like any other property. You can reference a layer by name (instead of referencing the active layer or a layer by id) via `{ _ref: 'layer', _name }` which u pass to the `_target` array:

```auto
const idx = photoshop.action.batchPlay([
    {
      _obj: 'get',
      _target: [{ _property: 'itemIndex' }, { _ref: 'layer', _name: *name* }],
    },
  ], { synchronousExecution: true })[0]['itemIndex']

```

Be aware that indices in PS are affected by the document having a background layer or not. To get correct results, you might need to add some logic like here:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/e/e9fcb811fc67e8d3b8fa627f662c81a7a54704d2.png)

---

<div class="post-metadata">

**Author:** ![liuxiao](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/liuxiao/32/1905_2.png) [@liuxiao](https://forums.creativeclouddeveloper.com/u/liuxiao)\
**Post date:** [September 29, 2021, 2:00am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/6 "2021-09-29T02:00:28Z")

</div>

Thank Simon  
I didn’t notice this field before ☹

---

<div class="post-metadata">

**Author:** ![rascheln](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@rascheln](https://forums.creativeclouddeveloper.com/u/rascheln)\
**Post date:** [February 4, 2023, 1:02am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/7 "2023-02-04T01:02:36Z")

</div>

Hello @simonhenke, I’m wondering: Is this still true?

I am also trying to move a layer, and I’m having trouble figuring out how I am to define the batchPlay descriptor to properly do so. I tried doing so just by name, but nothing happened. I was about to try this itemIndex approach, but it looks like such a property does not exist for the Layer object.

When I recorded a batchPlay{} for moving a layer, I got this in return:

```auto
{
   "_obj": "move",
   "_target": [
      {
         "_ref": "layer",
         "_enum": "ordinal",
         "_value": "targetEnum"
      }
   ],
   "to": {
      "_ref": "layer",
      "_index": 14
   },
   "adjustment": false,
   "version": 5,
   "layerID": [
      513
   ],
   "_isCommand": true
}

```

I am perplexed by this `_index` property. How do I access this? I feel that this was the crux of the solution for this thread, but I am pretty sure that layer descriptors no longer have a property called `itemIndex`, `index`, or anything of the sort.

If they do still have a descriptor, how do I get a list of all the properties that I can look at?

---

<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:** [February 7, 2023, 10:26pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/8 "2023-02-07T22:26:26Z")

</div>

The Layer descriptor still has the `itemIndex` property. Generally I wouldn’t expect batchPlay to change a lot, as it is just a layer on top of ActionManager code which hasn’t really changed much over the years - which is good, cause otherwise most plugins would break.

In my previous post I’ve shown how to access the `itemIndex` property. Moving to a specific index also works fine for me:

```auto
export function __moveLayerToIndex(_index: number) {
  return {
    _obj: 'move',
    _target: { _ref: 'layer', _enum: 'ordinal', _value: 'targetEnum' },
    to: { _ref: 'layer', _index },
    adjustment: false,
  }
}

```

That’s the descriptor I’m using. You can get rid of of stuff like `version`, `layerID` and `_isCommand` - as some threads here have shown, \_isCommand might even cause things to not work correctly.

And for checking out layer properties, use Alchemist or just log a layer descriptor via

```auto
{
    _obj: "get",
    _target: [
      { _ref: 'layer', _enum: 'ordinal', _value: 'targetEnum' }
    ],
}

```

→

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/5/5b1514b91a91be28fed2cb6469f1f7a6e3f17df5.png)

---

<div class="post-metadata">

**Author:** ![rascheln](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@rascheln](https://forums.creativeclouddeveloper.com/u/rascheln)\
**Post date:** [February 8, 2023, 12:54am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/9 "2023-02-08T00:54:05Z")

</div>

Thanks for this screenshot. I am clearly accessing the incorrect thing, as that is not what I am seeing when I attempt to access layer properties in the DOM. Of course, you said “Layer descriptor” so there is another level of information that I am missing. I am aware of action descriptors, and layer objects and properties, but not layer descriptors. You mention that you somehow use that object to “log a layer descriptor.” That looks like the kind of object that I would attach to a batchPlay(), but when I attempted that, nothing was returned.

Is there any way to access those properties in the console itself? This is what I get when I attempt to access layer properties in the console:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/3/3f4b6e4ffb94e7366e4c1f4e6d30192ab982ded9.png)

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [February 8, 2023, 4:55am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/10 "2023-02-08T04:55:04Z")

</div>

I believe that’s DOM result you’re seeing instead of BatchPlay command one

---

<div class="post-metadata">

**Author:** ![rascheln](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@rascheln](https://forums.creativeclouddeveloper.com/u/rascheln)\
**Post date:** [February 8, 2023, 5:59pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/11 "2023-02-08T17:59:04Z")

</div>

Oh, I see what you mean! Somehow, I had been entirely focused on the listener button. This is what I needed to look at, just clicking the inspector button and selecting a layer (for instance).

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/1/1d1ed86ecf37a6c62a52d8bae1be10dd47d7ed6c.png)

More information found [here](https://www.youtube.com/watch?v=qDw3mdGAG2o). (Davide Barranca’s UXP series vid #8)

This is that situation where he mentioned the limits of DOM vs. action manager coverage in video #6.

Thanks! I’ll see what I can do with these additional layer properties.

---

<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:** [February 8, 2023, 7:28pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/12 "2023-02-08T19:28:42Z")

</div>

It’s still weird that there isn’t any index or itemIndex property on a DOM layer object, isn’t it?  
Was that a design decision? Or maybe it has never been there, not sure since I didn’t use DOM a lot.

---

<div class="post-metadata">

**Author:** ![rascheln](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@rascheln](https://forums.creativeclouddeveloper.com/u/rascheln)\
**Post date:** [February 8, 2023, 9:19pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/13 "2023-02-08T21:19:16Z")

</div>

It’s very strange! Right now, I’m trying to transform a layer via the \_index property, but I seem to be having some trouble. I initially got this action descriptor to work by selecting via layer ID. However, I seem to be having trouble selecting by index (I wish to transform the layer below the selected layer)

Here’s the initial part of the action descriptor. “index” is successfully defined, but it does not treat this as a valid means to select the item.

```auto
{_obj: "transform",
    _target: [
      {
        _ref: "layer",
        _index: index
      }
    ]
...
}

```

---

<div class="post-metadata">

**Author:** ![DavideBarranca](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/davidebarranca/32/1523_2.png) [@DavideBarranca](https://forums.creativeclouddeveloper.com/u/DavideBarranca)\
**Post date:** [February 9, 2023, 8:49am UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/14 "2023-02-09T08:49:24Z")

</div>

No idea why it doesn’t work (target descriptors are notoriously weird, and some actions just don’t like some inputs). No idea whether the following works either, but if you’ve to act upon the layer below you could try with:

```auto
{
      _ref: "layer",
      _enum: "ordinal",
      _value: "previous" // or "next", or "back", or "forward", or even "prettyPlease"
}

```

Let us know!

---

<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:** [February 9, 2023, 3:11pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layers-with-batchplay/3039/15 "2023-02-09T15:11:45Z")

</div>

As far I know transform action works only with active item and ignores reference such as index, id, etc.

And that item can be layer, pixel selection, vector path. So you can have different outcomes based on active item.
