@Mohamed Hameed21513110
You can try the following script
deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
if (_name) {
_groupLength = Number(_name);
if (!isNaN(_groupLength)) {
if (_groupLength != 0 && _groupLength <= _layers.length) {
for (var i = 0; i < _groupLength; i++) {
selectLayerByID(_layers[i].id, true);
}
groupSelectedLayers(_name);
} else {
alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
}
} else {
alert('Enter value is not the number')
}
}
function groupSelectedLayers(theName) {
var desc159 = new ActionDescriptor();
var ref114 = new ActionReference();
var idlayer = stringIDToTypeID("layer");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
var idnull = stringIDToTypeID("null");
var idname = stringIDToTypeID("name");
ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
desc159.putReference(idnull, ref114);
desc159.putString(idname, "aaa");
executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
var desc63 = new ActionDescriptor();
var ref37 = new ActionReference();
ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
desc63.putReference(idnull, ref37);
var desc64 = new ActionDescriptor();
desc64.putString(idname, theName);
desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO)
}
function selectLayerByID(index, add) {
add = undefined ? add = false : add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
desc.putBoolean(charIDToTypeID("MkVs"), false);
try {
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
} catch (e) {
alert(e.message);
}
};
function deSelectAllLayers() {
var idselectNoLayers = stringIDToTypeID("selectNoLayers");
var desc26 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref3 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref3.putEnumerated(idLyr, idOrdn, idTrgt);
desc26.putReference(idnull, ref3);
executeAction(idselectNoLayers, desc26, DialogModes.NO);
}
Reference links
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-how-to-select-multi-layers-by-size-cm-or-inch/m-p/12550089/page/3#M602644
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-deselect-all-layers/td-p/9658958#:~:text=%3E%20Application%20Menus'.-,In%20the%20part%20below%2C%20go%20to%20'Select%20%3E%20Deselect%20Layers,Ctrl%2DAlt%2Dd'.&text=Now%20this%2C%20is%20the%20correct%20answer.
Note: The above script will not take care of nested structure of the layers.