cool, but I forgot about some nuances, sorry
Can you change this script so that it does not apply the action to the entire document, but only to the selected layers?...I think this will be the perfect way for the project 😃
// select layers the name of which ends in "_xx_xx";
// 2023, use it at your own risk;
if (app.documents.length > 0) {
// search layers with regexp;
var theLayers = collectLayersByNames([/_\d{1,2}_\d{1,2}$/], 2);
// process layers;
if (theLayers.length > 0) {
for (var d = 0; d < theLayers.length; d++) {
selectLayerByID(theLayers[d][0][2], false);
for (var e = 1; e < theLayers[d].length; e++) {
selectLayerByID(theLayers[d][e][2], true)
};
// group the active layer;
groupSelectedLayers("[slot] step" + String(theLayers[d][0][0].match(/_\d{1,2}_\d{1,2}$/)).replace(/_/g, "-"));
};
} else {alert ("no such layer")}
};
////////////////////////////////////
////// collect layers and/or groups with certain name, 0 layers, 1 groups, 2 both //////
function collectLayersByNames (theNames, layersOrGroups) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
var theCheck = false;
switch (Number(layersOrGroups)) {
case 0:
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart") {theCheck = true}
break;
case 1:
if (layerSet == "layerSectionStart") {theCheck = true}
break;
default:
if (layerSet != "layerSectionEnd") {theCheck = true}
break;
};
if (theCheck == true && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
// check name;
for (var x = 0; x < theNames.length; x++) {
var theNumbers = theName.match(theNames[x]);
if (theNumbers != null) {
var theCheck = false;
for (var y = 0; y < theLayers.length; y++) {
if (theNumbers == String(theLayers[y][0][0].match(theNames[x]))) {
theLayers[y].push([theName, theIndex, theID]);
theCheck = true;
};
};
if (theCheck == false) {
theLayers.push([[theName, theIndex, theID]])
}
}
}
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(theID,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), theID);
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);
}
};
////// group //////
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)
};
Quite frankly I think you may want to work on your manners.


// select selected layers the name of which ends in "_xx_xx" and group them in groups ending in the same numbers;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
// search layers with regexp;
var theLayers = collectSelectedLayersByNames([/_\d{1,2}_\d{1,2}$/], 2);
// process layers;
if (theLayers.length > 0) {
for (var d = 0; d < theLayers.length; d++) {
selectLayerByID(theLayers[d][0][2], false);
for (var e = 1; e < theLayers[d].length; e++) {
selectLayerByID(theLayers[d][e][2], true)
};
// group the active layer;
groupSelectedLayers("[slot] step" + String(theLayers[d][0][0].match(/_\d{1,2}_\d{1,2}$/)).replace(/_/g, "-"));
};
} else {alert ("no such layer")}
};
////////////////////////////////////
////// based on code by mike hale and paul riggott //////
function selectLayerByID(theID,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), theID);
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);
}
};
////// group //////
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)
};
////// collect selected layers matching names //////
function collectSelectedLayersByNames (theNames) {
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var thisIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
////////////////////////////////////
// check name;
for (var x = 0; x < theNames.length; x++) {
var theNumbers = theName.match(theNames[x]);
if (theNumbers != null) {
var theCheck = false;
for (var y = 0; y < selectedLayers.length; y++) {
if (theNumbers == String(selectedLayers[y][0][0].match(theNames[x]))) {
selectedLayers[y].push([theName, thisIndex, theIdentifier]);
theCheck = true;
};
};
if (theCheck == false) {
selectedLayers.push([[theName, thisIndex, theIdentifier]])
}
}
};
////////////////////////////////////
} catch (e) {};
};
// if only one:
}else{
alert ("only one layer selected");
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var thisIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
////////////////////////////////////
// check name;
for (var x = 0; x < theNames.length; x++) {
var theNumbers = theName.match(theNames[x]);
if (theNumbers != null) {
var theCheck = false;
for (var y = 0; y < selectedLayers.length; y++) {
if (theNumbers == String(selectedLayers[y][0][0].match(theNames[x]))) {
selectedLayers[y].push([theName, thisIndex, theIdentifier]);
theCheck = true;
};
};
if (theCheck == false) {
selectedLayers = [[[theName, thisIndex, theIdentifier]]]
}
}
};
////////////////////////////////////
} catch (e) {};
};
return selectedLayers;
};