Moving layers with BatchPlay?

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

          _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

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

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.