Why are you running both scripts? You will end up with a lot of duplicates.
You only need to run the second script or the new script below.
The first script add all layer names to keywords but not any layers that are in layersets.
The second scripts adds all layer names to keywords.
This script adds all layer names including layers in layersets and in smartobject to keywords...
[code]
#target photoshop;
app.bringToFront();
if(documents.length) main();
function main(){
Keys =[];
Smarts =[];
getNamesPlusIDs();
for(var s in Smarts){
selectLayerById(Smarts[s],false);
executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO );
if(activeDocument.layers.length >1) getNamesPlusIDs();
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmp = new XMPMeta( app.activeDocument.xmpMetadata.rawData );
//Uncomment the line below to clear the keywords field
//xmp.deleteProperty(XMPConst.NS_DC,'subject');
for(var s in Keys){
xmp.appendArrayItem(XMPConst.NS_DC, "subject", Keys[s].toString(), 0,XMPConst.PROP_IS_ARRAY);
}
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
function getNamesPlusIDs(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
for(i;i<count;i++){
if(i == 0) continue;
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
if(layerName.match(/^<\/Layer group/) ) continue;
var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
if(!isLayerSet) Keys.push(layerName);
if(desc.hasKey(stringIDToTypeID( 'smartObject'))) Smarts.push(Id);
}
}
function selectLayerById(id,add){
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
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){}
};
};
[/code]