Skip to main content
Innotex Nico Tanne
Inspiring
April 3, 2025
Answered

Move a Group into a Group with Layerindex

  • April 3, 2025
  • 1 reply
  • 483 views

Hello dear community,

 

Unfortunately I'm stuck at the moment and need your help.

 

I have a script in which I have a layer and save the layer index of this layer and whether this is possibly in a group:

var originalLayer = app.activeDocument.activeLayer;
function findLayerIndex(layer, parentGroup, parentName) {
    for (var i = 0; i < parentGroup.length; i++) {
        if (parentGroup[i] === layer) {
            return { index: i, group: parentName };
        }
        if (parentGroup[i].typename === "LayerSet") {
            var result = findLayerIndex(layer, parentGroup[i].layers, parentGroup[i].name);
            if (result) return result;
        }
    }
    return null;
}

// Get Doc & Layer
var doc = app.activeDocument;
var layers = doc.layers;
var originalLayer = doc.activeLayer; // Active layer
var result = findLayerIndex(originalLayer, layers, "Root");

if (result) {
    var layerIndex = result.index;
    var parentGroup = result.group;
   // alert("Layer-Index: " + layerIndex);
   // alert("Gruppe gespeichert: " + parentGroup);
} else {
    alert("no Layer found");
}

Then I just do some copys of them and put them in a new group, this is then created at the top of all levels. Then I want to move this group to the original position. if the level was not within a group, everything works correctly. I just can't get it to move the new group back into the old group and the correct position. do you have any ideas?

  
if (parentGroup !== "Root") {
    alert("here i need the code to move my Group in the saved Group and index position  ")
    
 

}else{
        // Move Group to Root index
        doc.activeLayer = group;
        //alert("Group Selected")
        while (doc.activeLayer != layers[layerIndex]) {
            doc.activeLayer.move(layers[layerIndex], ElementPlacement.PLACEAFTER);};
};

 Thank you for your help 🙏

Correct answer c.pfaffenbichler

Hello Paffenbichler,

 

Here is my light script for testing:

As I said, if the layer is not in a group, it moved back to the correct layer position.

If the layer is in a group, it will unfortunately not be moved back into the group

😞 

// Get Layer
var originalLayer = app.activeDocument.activeLayer;

// Get Index
function findLayerIndex(layer, parentGroup, parentName) {
for (var i = 0; i < parentGroup.length; i++) {
    if (parentGroup[i] === layer) {
        return { index: i, group: parentName };
    }
    if (parentGroup[i].typename === "LayerSet") {
        var result = findLayerIndex(layer, parentGroup[i].layers, parentGroup[i].name);
        if (result) return result;
    }
}
return null;
}


var doc = app.activeDocument;
var layers = doc.layers;
var originalLayer = doc.activeLayer; 
var result = findLayerIndex(originalLayer, layers, "Root");

if (result) {
var layerIndex = result.index;
var parentGroup = result.group;
// alert("Layer-Index: " + layerIndex);
// alert("Group: " + parentGroup);
} else {
alert("Layer not found");
}


var group = app.activeDocument.layerSets.add();

  
originalLayer.move(group, ElementPlacement.INSIDE);

   if (parentGroup !== "Root") {
    alert("here i need the code to move my Group in the saved Group and index position  ")
 // Move Group
 doc.activeLayer = group;
 //alert("Group Selected")
 while (doc.activeLayer != layers[layerIndex]) {
     doc.activeLayer.move(layers[layerIndex], ElementPlacement.PLACEAFTER);};

}else{
        // Move Group
        doc.activeLayer = group;
        //alert("Group Selected")
        while (doc.activeLayer != layers[layerIndex]) {
            doc.activeLayer.move(layers[layerIndex], ElementPlacement.PLACEAFTER);};
};

 


Thanks. 

 

If I understand the process correctly I think moving the Group first, then the Layer might suffice to address the problem. 

// Get Layer
var originalLayer = app.activeDocument.activeLayer;

// Get Index
function findLayerIndex(layer, parentGroup, parentName) {
for (var i = 0; i < parentGroup.length; i++) {
    if (parentGroup[i] === layer) {
        return { index: i, group: parentName };
    }
    if (parentGroup[i].typename === "LayerSet") {
        var result = findLayerIndex(layer, parentGroup[i].layers, parentGroup[i].name);
        if (result) return result;
    }
}
return null;
}


var doc = app.activeDocument;
var layers = doc.layers;
var originalLayer = doc.activeLayer; 
var result = findLayerIndex(originalLayer, layers, "Root");

if (result) {
var layerIndex = result.index;
var parentGroup = result.group;
// alert("Layer-Index: " + layerIndex);
// alert("Group: " + parentGroup);
} else {
alert("Layer not found");
}

var group = app.activeDocument.layerSets.add();
group.move(originalLayer, ElementPlacement.PLACEBEFORE);
  
originalLayer.move(group, ElementPlacement.INSIDE);

 

1 reply

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2025

I am not sure I follow. 

 

Layer’s index can be tricky because when you create new Layers that can change the index, depending on the position in the stack. 

As you appear to be using DOM code anyway why not set up a variable for the Group right away? 

Innotex Nico Tanne
Inspiring
April 3, 2025

Hello Pfaffenbichler,

 

To be honest, I used chat GPT a bit.

I had only seen that the index (root) differs from the index of the Group. as long as I do not have to move the newly created group back into a group, everything works fine. only the group is not moved at all when it is to be moved into a group. i have already played around a lot and just don't understand it 😞

c.pfaffenbichler
Community Expert
Community Expert
April 3, 2025

If what I told you does not help you then snippets of code seem insufficient for meaningful trouble-shooting. 

Please provide the Scripts and a sample file.