Moving Layer via itemIndex

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}.

//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();
//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

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

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

//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())

}

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

2 Likes

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.

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

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

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

the script doesn’t do much except:

Debug:

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.

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

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.

1 Like

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

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

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