# Move Layer Bug

**URL:** <https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458>\
**Category:** UXP Plugin API\
**Tags:** bug\
**Created:** [August 5, 2021, 11:10pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458 "2021-08-05T23:10:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![blastframe](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blastframe/32/1519_2.png) [@blastframe](https://forums.creativeclouddeveloper.com/u/blastframe)\
**Post date:** [August 5, 2021, 11:10pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/1 "2021-08-05T23:10:20Z")

</div>

Hello,  
I’m encountering some strange behavior when moving layers. This happens for me when using `batchPlay` (as in the example below) and `moveAbove()`.

```auto
const move_test = async() =>
{
  const ps = require("photoshop").app;
  let doc = ps.activeDocument;
  const batchPlay = require("photoshop").action.batchPlay;

  //assumes two layers named Layer 1 & Layer 2, respectively.
  //Layer 2 is in front of Layer 1

  const result = await batchPlay(
  [
     {
        "_obj": "select",
        "_target": [
           {
              "_ref": "layer",
              "_name": "Layer 1"
           }
        ],
        "makeVisible": false,
        "_isCommand": true,
        "_options": {
           "dialogOptions": "dontDisplay"
        }
     },
     //duplicate "Layer 1"
     {
       "_obj": "duplicate",
       "_target": [{
         "_ref": "layer",
         "_enum": "ordinal",
         "_value": "targetEnum"
       }],
       "version": 5,
       "_isCommand": true,
       "_options": {
         "dialogOptions": "dontDisplay"
       }
     },
     //move "Layer 1 copy" to front
     {
       "_obj": "move",
       "_target": [{
         "_ref": "layer",
         "_enum": "ordinal",
         "_value": "targetEnum"
       }],
       "to": {
         "_ref": "layer",
         "_enum": "ordinal",
         "_value": "front"
       },
       "_isCommand": true,
       "_options": {
         "dialogOptions": "dontDisplay"
       }
     }
  ],{
     "synchronousExecution": true, //I have tried with true & false
     "modalBehavior": "fail"
  });

  console.log("result",result)
}

```

The `front` value is inconsistent.

Perhaps related, other move issues have appeared on this forum. @simonhenke mentioned issues when `doc.hasBackgroundLayer` is `true`. `moveAbove()` & `moveBelow()` also have issues:  
[Weird Layer.moveBelow() behavior](http://forums.creativeclouddeveloper.com/t/weird-layer-movebelow-behavior/2280)

Thank you.

---

<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:** [August 6, 2021, 9:26am UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/2 "2021-08-06T09:26:20Z")

</div>

There’s definitely something weird going on. Your `async/await` and `synchronousExecution` code is fine and the background layer isn’t the problem either.

Here’s my observation:

It **doesn’t** work when

- the top layer is selected
- no layer is selected

It **does** work when:

- any layer except the top one is selected
- you split up the code into two batchPlay calls (1: select layer, 2: duplicate & move)

To me this looks like a bug and I’ve also encountered some weird behavior when moving layers in my own plugins. I always just try to work around them and do a lot of testing.

---

<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:** [August 6, 2021, 10:39am UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/3 "2021-08-06T10:39:12Z")

</div>

I ended up using Batch play  
Here’s a descriptor to move layers:

```auto
    {
        _obj: 'move',
        _target: [{_ref: 'layer', _enum: 'ordinal', _value: 'targetEnum'}],
        to: {_ref: 'layer', _index: indexToMoveTo},
        adjustment: false,
        layerID: arrayLayerIDsToMove
    }

```

I think without `_target` it didn’t work, but it doesn’t really do anything here. Don’t ask, it’s been a while I worked with 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:** [August 6, 2021, 11:08am UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/4 "2021-08-06T11:08:47Z")

</div>

I think `_target` is necessary, `layerID` can be omitted though, if I’m not mistaken.

However, I don’t think it solves this thread’s issue - it’s just moving to a specific index instead of to the front.

---

<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:** [August 6, 2021, 11:40am UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/5 "2021-08-06T11:40:09Z")

</div>

> [@simonhenke](#):
>
> I think `_target` is necessary, `layerID` can be omitted though, if I’m not mistaken.

Is there a way with `_target` to target multiple IDs?

* * *

> [@simonhenke](#):
>
> it’s just moving to a specific index instead of to the front.

But it’s easy to get the top layer index

```auto
    const doc = getDocById(docId);
    const topLayer = doc.layerTree[0];
    const topIndex = getLayerProperty(topLayer._id, 'itemIndex');

```

I use this logic in my [_Pinned Layers_](https://exchange.adobe.com/creativecloud/plugindetails.html/app/cc/7682b070) plugin and it seems to be working fine. Also, IIRC, this kind of descriptor, using `layerID` was suggested to me by @kerrishotts when I just had too many issues with renaming, activating, moving layers in the same action.

---

<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:** [August 6, 2021, 2:00pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/6 "2021-08-06T14:00:51Z")

</div>

> [@Karmalakas](#):
>
> Is there a way with `_target` to target multiple IDs?

Yes, for example:

```auto
export function __selectByIDs(ids: number[]) {
  return {
    _obj: 'select',
    _target: ids.map(_id => ({_ref: 'layer', _id})),
    selectionModifier: {
      _enum: 'selectionModifierType',
      _value: 'addToSelection',
    },
    makeVisible: false,
  }
}

```

* * *

> [@Karmalakas](#):
>
> But it’s easy to get the top layer index

Yes, but in theory the `{"_ref": "layer","_enum": "ordinal","_value": "front"}` approach should do the same in less LoC.  
**However, I quickly tested both versions and can confirm, that moving layers to a specific index (for example the top one) is not affected by the bug described above, and therefore a valid workaround.**  
I think that was also what I did for my plugins, when I realized the “`front`” moving thing didn’t work.

> [@Karmalakas](#):
>
> Also, IIRC, this kind of descriptor, using `layerID` was suggested to me by @kerrishotts when I just had too many issues with renaming, activating, moving layers in the same action

You can run a simple test: Try to add a valid layerID there and see if it’s actually the one getting moved. I did just that, and as expected, it’s the selected layer getting moved (due to `{_ref: 'layer', _enum: 'ordinal', _value: 'targetEnum'}`), not the one with the specified ID.

In fact, you can write anything there, for example `layerID: ["HELLO"]` and it won’t break as it simply gets ignored. That’s just additional info Photoshop adds to the returned descriptor when you listen to events.

---

<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:** [August 6, 2021, 2:29pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/7 "2021-08-06T14:29:27Z")

</div>

> [@simonhenke](#):
>
> You can run a simple test: Try to add a valid layerID there and see if it’s actually the one getting moved. I did just that, and as expected, it’s the selected layer getting moved (due to `{_ref: 'layer', _enum: 'ordinal', _value: 'targetEnum'}` ), not the one with the specified ID.

I’ll revisit my plugin code then (it was way overdue anyway), because it moves the provided IDs to the top no matter what’s selected 🤔 At least it used to 😅

---

<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:** [August 6, 2021, 4:08pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/8 "2021-08-06T16:08:32Z")

</div>

Are you sure, that the provided IDs weren’t also the selected ones already?  
I mean not before running the script, but maybe you selected them during the process.

---

<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:** [August 6, 2021, 4:31pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/9 "2021-08-06T16:31:53Z")

</div>

I’m pretty sure, but I’ll get back to you probably tomorrow 🙂 Will try to figure out what’s happening 🙂 It’s a quite complex logic of that plugin, although it does a pretty simple thing

---

<div class="post-metadata">

**Author:** ![blastframe](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blastframe/32/1519_2.png) [@blastframe](https://forums.creativeclouddeveloper.com/u/blastframe)\
**Post date:** [August 7, 2021, 12:30am UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/10 "2021-08-07T00:30:39Z")

</div>

Thank you all for the replies. Combining your feedback, I came up with this…

```auto
const move_multiple_above = (tgt_id, ids) => {
  return {
    _obj: 'move',
    _target: ids.map(_id => ({_ref: 'layer',_id})),
    to: {
      _ref: 'layer',
      _index: tgt_id
    },
    adjustment: false
  }
}

let target_index = 3; //where to move the selected layers
let ids = doc.activeLayers.map(x => x._id); //add selected layer ids to 'ids' array 

const result = await batchPlay(
[
  move_multiple_above(target_index,ids) //gets descriptor for moving multiple layers
],{
  "synchronousExecution": true,
  "modalBehavior": "fail"
});

```

It’s kinda slow, unfortunately. I really wish `front` worked with the `move` `batchPlay`! Does anyone have some suggestions for better performance? Thank you.

---

<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:** [August 7, 2021, 2:08pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-bug/3458/11 "2021-08-07T14:08:20Z")

</div>

Simon, you were right. I was looking a bit at the wrong part of flow. Before running that descriptor I select the layers I need. Removed `layerID` and nothing changed 🙂 Thanks for pointing that out
