Answered
[ExtendScript] How to move layer children while preserving their order
- September 8, 2025
- 2 replies
- 350 views
I'm writing code to move the contents of a layer to another layer. Moving pageItems alone works fine, but when sublayers and pageItems are at the same level, I don't know how to move them while preserving their order.
As far as I know, the only way is to retrieve pageItems and layers separately, which results in two clusters after moving—like beer liquid and foam. How to solve this problem?
(function() {
if(app.documents.length <= 0) {return ;}
var doc = app.documents[0] ;
var srcLayer = doc.layers[1] ;
var dstLayer = doc.layers[0] ;
var pageItems = srcLayer.pageItems ;
for(var i = pageItems.length - 1 ; i >= 0 ; i--) {
pageItems[i].move(dstLayer, ElementPlacement.PLACEATEND) ;
}
var sublayers = srcLayer.layers ;
for(var i = sublayers.length - 1 ; i >= 0 ; i--) {
sublayers[i].move(dstLayer, ElementPlacement.PLACEATEND) ;
}
})() ;
Update:
Added the test data (.ai) and before/after image.

