# Moving Layer via itemIndex

**URL:** <https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210>\
**Category:** UXP Plugin API\
**Created:** [January 26, 2022, 8:19am UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210 "2022-01-26T08:19:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanBlack](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ryanblack/32/1911_2.png) [@RyanBlack](https://forums.creativeclouddeveloper.com/u/RyanBlack)\
**Post date:** [January 26, 2022, 8:19am UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/1 "2022-01-26T08:19:26Z")

</div>

Ok, I’m having one heck of a time with this, and while I’ve hesitated to ask, I’m going on nearly 1 full day trying to figure this out. I can get consoles, but when I try to use the value I get Promise {: undefined}.

```auto
//get layer itemIndex
async function itmIdx() {

   const getIndex = await batchPlay(

      [{
         _obj: "get",
         _target: [
            {
               _property: "itemIndex"
            },
            {
               _ref: "layer",
               _name: "Pen Out"
            }
         ],
         _options: {
            dialogOptions: "dontDisplay"
         }
      }
      ], {});

   const value = getIndex[0].itemIndex - 1;
   console.log(value);
}

let idx = itmIdx();

```

```auto
//Move Layer Pen Out to itemIndex
const penOut = async () => {
   await batchPlay([

      console.log(idx)

   ], {})

}

```

My goal is to use the value to move a layer into a layer folder. using -1. However I’m getting no where when dropping idx into the obj “move”, \_index: idx

---

<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:** [January 26, 2022, 8:24am UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/3 "2022-01-26T08:24:29Z")

</div>

I found `_index` to be completely unreliable when used in cases like this. I only calculate positions depending on it, but for moving or selecting some other layer in some position I use `_id`. Eg. find a layer you need by `_index`, but then use `_id` for any operations on that layer you found

---

<div class="post-metadata">

**Author:** ![RyanBlack](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ryanblack/32/1911_2.png) [@RyanBlack](https://forums.creativeclouddeveloper.com/u/RyanBlack)\
**Post date:** [January 26, 2022, 8:41am UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/5 "2022-01-26T08:41:03Z")

</div>

I’m tired or something, I’m sure I’m screwing it up lol.

```auto
//get layer itemIndex
async function itmIdx() {

   const getIndex = await batchPlay(

      [{
         _obj: "get",
         _target: [
            {
               _property: "itemIndex"
            },
            {
               _ref: "layer",
               _name: "Pen Out"
            }
         ],
         _options: {
            dialogOptions: "dontDisplay"
         }
      }
      ], {});

   return getIndex[0].itemIndex - 1;
};

const idx = itmIdx();

//Move Layer Pen Out to itemIndex
const penOut = async () => {

   await batchPlay([

      {
         "_obj": "duplicate",
         "_target": [
            {
               "_ref": "layer",
               "_enum": "ordinal",
               "_value": "targetEnum"
            }
         ],
         "name": "PenOut",
         "version": 5,
         "_isCommand": true
      },

      {
         "_obj": "rasterizeLayer",
         "_target": [
            {
               "_ref": "layer",
               "_enum": "ordinal",
               "_value": "targetEnum"
            }
         ],
         "_isCommand": true
      },

      {
         "_obj": "move",
         "_target": [
            {
               "_ref": "layer",
               "_enum": "ordinal",
               "_value": "targetEnum"
            }
         ],
         "to": {
            "_ref": "layer",
            "_id": idx
         },
         "adjustment": false,
         "version": 5,
         "layerID": [
            138
         ],
         "_isCommand": true
      },

      {
         "_obj": "select",
         "_target": [
            {
               "_ref": "penTool"
            }
         ],
         "dontRecord": true,
         "forceNotify": true,
         "_isCommand": false
      },

   ], {})

   console.log(itmIdx())

}

```

---

<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:** [January 26, 2022, 11:29am UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/6 "2022-01-26T11:29:15Z")

</div>

What is the result of this? (vs. the expected result)

---

<div class="post-metadata">

**Author:** ![RyanBlack](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ryanblack/32/1911_2.png) [@RyanBlack](https://forums.creativeclouddeveloper.com/u/RyanBlack)\
**Post date:** [January 26, 2022, 4:50pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/7 "2022-01-26T16:50:47Z")

</div>

The desired outcome is for the layer to move to the value captured by itmIdx().

When I execute it with a number value the function works no problem.

```auto
            "to": {
               "_ref": "layer",
               "_index": 10
            },

```

however as soon as I insert my const idx into the value

```auto
            "to": {
               "_ref": "layer",
               "_index": idx
            },

```

the script doesn’t do much except:

Debug:

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

Also I notice that if I move the layer and run the console again, the result is the same. I only get an accurate index when I first save my code. I’ve temporarily fixed this by inserting the function into my event listened function.

---

<div class="post-metadata">

**Author:** ![RyanBlack](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ryanblack/32/1911_2.png) [@RyanBlack](https://forums.creativeclouddeveloper.com/u/RyanBlack)\
**Post date:** [January 26, 2022, 6:12pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/8 "2022-01-26T18:12:24Z")

</div>

OK, I fixed this … I trashed my code and re-did it. Here’s what I’m using incase it helps someone else.

```auto
const penOut = async () => {

   const batchPlay = require("photoshop").action.batchPlay;

   const result = await batchPlay(
      [
         {
            _obj: "get",
            _target: [
               {
                  _ref: "layer",
                  _name: "Pen Out Folder"
               }
            ],
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ], {
      synchronousExecution: false,
      modalBehavior: "wait"
   });
   const idx = result[0].itemIndex;

   console.log(idx)

   const movepen = await batchPlay(
      [
         {
            _obj: "duplicate",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            version: 5,
            ID: [
               168
            ],
            _isCommand: true,
            _options: {
               dialogOptions: "dontDisplay"
            }
         },
         {
            _obj: "rasterizeLayer",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            _isCommand: true,
            _options: {
               dialogOptions: "dontDisplay"
            }
         },
         {
            _obj: "set",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            to: {
               _obj: "layer",
               name: "Pen"
            },
            _isCommand: true,
            _options: {
               dialogOptions: "dontDisplay"
            }
         },
         {
            _obj: "move",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            to: {
               _ref: "layer",
               _index: idx
            },
            adjustment: false,
            version: 5,
            layerID: [
               168
            ],
            _isCommand: true,
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ], {
      synchronousExecution: false,
      modalBehavior: "wait"
   });

```

Always happy to clean things up if someone sees something I missed.

---

<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:** [January 26, 2022, 11:07pm UTC](https://forums.creativeclouddeveloper.com/t/moving-layer-via-itemindex/4210/9 "2022-01-26T23:07:00Z")

</div>

If you want to clean up the code even more, you can remove all occurrences of

```auto
version: 5,
layerID: [
   168
],
_isCommand: true,
_options: {
   dialogOptions: "dontDisplay"
}

```

The first three don’t have any effect and `dialogOptions: "dontDisplay"` is the default.

Also, getters don’t really need to be async and for better performance you can directly get only the itemIndex property (not the whole descriptor), so you could write it like

```auto
const idx = batchPlay([
     {
        _obj: "get",
        _target: [
          {
            _property: "itemIndex"
          },
          {
            _ref: "layer",
            _name: "Pen Out Folder"
          }
        ],
        _options: {
          dialogOptions: "dontDisplay"
        }
     }
  ], { synchronousExecution: true })[0]["itemIndex"];

```
