Not that you wanted, as you can't select many layers with my script, but just one or a group of layers what after all has to create new layer over selected layer or in selected group, but maybe you can play with it for your needs that you make somehow it will be working selecting more than one layer or/and group. Check the post from Fri Nov 18, 2016 5:45 pm in there: Creating and moving an artLayer inside "active" layerSet - Page 3 - PS-SCRIPTS.COM
I think most easy solution could be:
1) Group all selected layers (ctrl + g)
var idMk = charIDToTypeID( "Mk " ); var desc24 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref20 = new ActionReference(); var idlayerSection = stringIDToTypeID( "layerSection" ); ref20.putClass( idlayerSection ); desc24.putReference( idnull, ref20 ); var idFrom = charIDToTypeID( "From" ); var ref21 = new ActionReference(); var idLyr = charIDToTypeID( "Lyr " ); var idOrdn = charIDToTypeID( "Ordn" ); var idTrgt = charIDToTypeID( "Trgt" ); ref21.putEnumerated( idLyr, idOrdn, idTrgt ); desc24.putReference( idFrom, ref21 ); var idlayerSectionStart = stringIDToTypeID( "layerSectionStart" ); desc24.putInteger( idlayerSectionStart, 17 ); var idlayerSectionEnd = stringIDToTypeID( "layerSectionEnd" ); desc24.putInteger( idlayerSectionEnd, 18 ); var idNm = charIDToTypeID( "Nm " ); desc24.putString( idNm, """Group 1""" ); executeAction( idMk, desc24, DialogModes.NO );
2) Now your active layer is only group and contains everything as you said
3) Go recursive all layers and folders (recursive code is in "Delete all empty layers" script (packed with Photoshop)
4) Remember layer ID for every single layer and folder within recursive call (NOT layer index) (push layer into array)
5) Step back in history
var idslct = charIDToTypeID( "slct" ); var desc26 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref24 = new ActionReference(); var idHstS = charIDToTypeID( "HstS" ); var idOrdn = charIDToTypeID( "Ordn" ); var idPrvs = charIDToTypeID( "Prvs" ); ref24.putEnumerated( idHstS, idOrdn, idPrvs ); desc26.putReference( idnull, ref24 ); executeAction( idslct, desc26, DialogModes.NO );
6) Your loop with array and function inside. You need select each layer one by one and select layer by ID (NOT index).
The silly thing is that most function and methods works only with one selected layer. And rarely with multiple selected layers.
If you are masochist and you need great performance, you can check my script "Delete all empty layers faster". It deletes all groups where are no layers or layers with zero dimensions or groups which contains groups without layers which are not locked. It doesn't use recursion and undos. Magic scripts for Photoshop