I ignored your last point but the other things should be addressed.
// 2023, use it at your own risk;
// Define the search terms
var searchTerms = ["BOOK", "COMP"];
// Get a reference to the active document;
var doc = app.activeDocument;
// Check if the "FINAL" group exists, and create it if it doesn't;
var finalGroup;
try {
finalGroup = doc.layerSets.getByName("FINAL");
doc.activeLayer = finalGroup;
} catch (e) {
finalGroup = doc.layerSets.add();
finalGroup.name = "FINAL";
};
var finalId = getLayerIndex ();
// Loop through all layers and groups in the document;
var theLayers = collectLayersByNames (searchTerms);
for (var i = 0; i < theLayers.length; i++) {
moveLayerTo(theLayers[i][2], finalId);
};
////////////////////////////////////
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// the file;
var myDocument = app.activeDocument;
// 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;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
for (var x = 0; x < theNames.length; x++) {
if (theName.indexOf(theNames[x]) != -1) {theLayers.push([theName, theIndex, theID])}
}
};
}
catch (e) {};
};
return theLayers
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// based on code by mike hale and paul riggott //////
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);
}
};
////// by mike hale, via paul riggott //////
function getLayerIndex(){
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('itemIndex'));
// ref.putIdentifier(charIDToTypeID("Lyr "), theId);
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
d = executeActionGet(ref);
return (d.getInteger(stringIDToTypeID('itemIndex'))-1);
};