# Move Layer To The Top

**URL:** <https://forums.creativeclouddeveloper.com/t/move-layer-to-the-top/2608>\
**Category:** UXP Plugin API\
**Created:** [March 2, 2021, 5:20pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-to-the-top/2608 "2021-03-02T17:20:19Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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 3, 2021, 2:05pm UTC](https://forums.creativeclouddeveloper.com/t/move-layer-to-the-top/2608/5 "2021-03-03T14:05:35Z")

</div>

Hello!  
I have been using both approach a lot in my scripts, and I feel like sharing one thing or two about moving a layer to the top.

Option #1:

```
var currentDocument = app.activeDocument;
var layers = currentDocument.activeLayers;
var SelectedLayer = layers[0];
SelectedLayer.moveAbove(currentDocument.layerTree[0]);

```

Option #2:

```
batchPlay(
[
   {
      "_obj": "move",
      "_target": [
         {
            "_ref": "layer",
            "_enum": "ordinal",
            "_value": "targetEnum"
         }
      ],
      "to": {
         "_ref": "layer",
         "_enum": "ordinal",
         "_value": "front"
      },
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{ });

```

In fact, both options seems to be equivalent at first, but they yeild different results, especially if you have nested Groups & Layers in the PS layers stack.  
Let’s have a closer look.  
.  
.

→ **Case 1**  
PS stack with a bunch of layers, one group and a layer inside:

 ![Screenshot 2021-03-03 at 13.39.22](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/c/c98f95a86eaa4eef95a4121d1f539ad0654c3b4c.jpeg)

Let’s say we want to move the Yellow layer “Layer 3” to the top.

Using Option #1 _ **or** _ Option #2 will give the _same_ result:

 ![Screenshot 2021-03-03 at 13.39.26](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/1/1dd92ec9a34af49de3f65b8acf89d44cc78ba8e6.jpeg)  
Indeed, moving the yellow “Layer 3” to the top is done in the _same fashion with either Option #1 or Option #2._  
That’s good 🙂

.  
.

Now, let’s examine the case where Option #1 and Option #2 begins to show different results.

→ **Case 2**  
Consider this layer stack with nested groups:

 ![Screenshot 2021-03-03 at 13.29.13](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/4/4d855d0cb5db143516afcebcee57fa2f8e23d8b2.jpeg)

Let’s say we want to move the Red layer “Layer 3” to the top.

This is the result when using Option #1

 ![Screenshot 2021-03-03 at 13.29.40](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/8/89258ffa164e06227db509980a039aac5c4f14c2.jpeg)

.

And this is the result when using Option #2

 ![Screenshot 2021-03-03 at 13.29.20](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/0/04d87b8c0155f7fe06b12d1698bb5fe3ed0ec452.jpeg)

A-ha! 😅  
As you can see, Option #1 and Option #2 are not equivalent anymore.  
Beware of nested Groups & Layers structure.  
.  
.

→ **Conculsion** :

- Option #2 moves “Layer 3” to the top, yes, but to the top of the group it was contained in. Note: If you’d wat to move it at the very top of the PS stack, you’d have to call batchPlay Option#2 twice (in this particular case because of nested levels).

- On the other hand, Option #1 moves “Layer 3” to the top of the layer stack _itself_.  
If you want to be extra-sure that “Layer 3” will be move to the top regardless of the PS stack structure, then Option #1 is probably your best friend.

Personally, I consider Option #1 to be the safest. It does what it should be doing: move the layer to the (very) top, no matter how the PS stack looks like.

Finally, it depends on your particular need and expectations.

Hope this helps 🙂

---

_[View the full topic](https://forums.creativeclouddeveloper.com/t/move-layer-to-the-top/2608)._
