Skip to main content
Known Participant
Apr 7, 2025 4:43 PM UTC
Question

Moving layers and folders out of a folder to the root via script

I want to take a load of layers and layersets out of a folder so they're no longer nested. I'm using this

// Move art layers out of the STACK folder
for (var i = duplicatedStackFolder.artLayers.length - 1; i >= 0; i--) {
    duplicatedStackFolder.artLayers[i].move(newStackDoc, ElementPlacement.PLACEATEND);
}

// Move layer sets (subfolders) out of the STACK folder in forward order
for (var j = 0; j < duplicatedStackFolder.layerSets.length; j++) {
    duplicatedStackFolder.layerSets[0].move(newStackDoc, ElementPlacement.PLACEATEND);
}

 

My problem is some of the layersets get missed and not taken out of the folder.

If i use this instead then i get all the layersets but they end up in the wrong order.

// Move layer sets (subfolders) out of the STACK folder
for (var j = duplicatedStackFolder.layerSets.length - 1; j >= 0; j--) {
    duplicatedStackFolder.layerSets[j].move(newStackDoc, ElementPlacement.PLACEATEND);
}

 I've tried using PLACEATSTART but that doesn't seem to work

 

how can i resolve this?

11 replies

jduncan
Community Expert
Community Expert
Apr 7, 2025 10:05 PM UTC

Do you just want the immediate items (first level) of the group layer moved out of the group? If so, you can just move each item like this...

// grab current document
doc = app.activeDocument;

// layer you want to move items out of
var nestedGroupLayer = doc.activeLayer;

// layer you want the moved items placed in reference to
var moveTargetLayer = doc.layers[0];

// iterate over all of the group layer items and move them out
for (var i = nestedGroupLayer.layers.length - 1; i >= 0; i--) {
    nestedGroupLayer.layers[i].move(moveTargetLayer, ElementPlacement.PLACEAFTER);
}

 

 

Or if you want to extract all layers from all nested layer sets you can do it recursively like this...

function moveLayers(layerSet, layerRef) {
    for (var i = layerSet.layers.length - 1; i >= 0; i--) {
        if (layerSet.layers[i].typename == "LayerSet") {
            moveLayers(layerSet.layers[i], layerRef);
        } else {
            layerSet.layers[i].move(layerRef, ElementPlacement.PLACEAFTER);
        }
    }

    // remove empty layer set
    layerSet.remove();
}

// grab current document
doc = app.activeDocument;

// layer you want to move items out of
var nestedGroupLayer = doc.activeLayer;

// layer you want the moved items placed in reference to
var moveTargetLayer = doc.layers[0];

// recursively move all layers out of group layer
moveLayers(nestedGroupLayer, moveTargetLayer);

 

Let me know if this works or if I misunderstood you?

Translate
Stephen Marsh
Community Expert
Community Expert
Apr 7, 2025 8:55 PM UTC

Have you tried deleting the parent layer group without deleting the contents (presuming that the parent group is no longer required)?

 

EDIT: Assuming that you have the target group selected:

 

if (app.activeDocument.activeLayer instanceof LayerSet) {
	function s2t(s) {
		return app.stringIDToTypeID(s);
	}
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	descriptor.putReference(s2t("null"), reference);
	descriptor.putBoolean(s2t("deleteContained"), false);
	executeAction(s2t("delete"), descriptor, DialogModes.NO);
}

 

Or ungrouping for the same result:

if (app.activeDocument.activeLayer.typename == "LayerSet") {
	app.runMenuItem(stringIDToTypeID("ungroupLayersEvent"));
}

 

Translate
Known Participant
Apr 7, 2025 11:09 PM UTC

This works great thanks, didn't think about ungrouping.
I do have a stange problem though, any loose layers are made visible, any idea how i can retain visibilty?
Whats strange is when i go back in the history the layer is off but the result shows it clealy visible

Translate
Stephen Marsh
Community Expert
Community Expert
Apr 8, 2025 12:01 AM UTC

I'm guessing that it's just a byproduct of ungrouping... If you use one of the methods outlined by @jduncan is layer visibility also affected?

 

EDIT: I just tested, and child layer visibility isn't affected when ungrouping or deleting the parent group.

Translate