What's new

Scripting Pull Layer Names for File Info Keywords


Amywhiteman

New Member
Messages
4
Likes
0
Hi All,
I am trying to streamline the digital asset management process (woo!). I am trying to figure out a way to write a script that will pull all file names from my photoshop document and put them into the File Info as keywords. Someone on the Adobe forums graciously helped me out with a script that pulls the top layer names from my photoshop document. But I am looking to pull all the layer names not just the top. Let me know if you think it is possible!

Thanks!!

Here is the Javascript
#target photoshop

// community.adobe.com/t5/Photoshop/Copy-photoshop-layer-names-and-paste-into-IPTC-keywords/m-p/10714597

/*
// Active layer name to keywords
var doc = app.activeDocument;
var layerName = doc.activeLayer.name;
var layerNameArray = new Array(layerName);
doc.info.keywords = layerNameArray;
alert('Keywords overwritten with active layer name');
*/

// gist.githubusercontent.com/vladocar/1628924/raw/a486566b2a648f94399fffa67dc9a2b4d681b86e/layerNames.js
// Get the top level layer names for groups and layers, layers in groups are ignored
var layerNum = app.activeDocument.layers.length;
var allLayers = [];
for (var i = 0; i < layerNum; i++) {
allLayers = app.activeDocument.layers.name; // RegEx placeholder - name.replace(/find/gi, 'replace')
}
// Add the top level layer names to keywords
app.activeDocument.info.keywords = allLayers;

alert('Keywords overwritten with top-level layer names!');
 

Paul MR

The Script Master
Messages
382
Likes
205
Please try this...
Code:
#target photoshop;
app.bringToFront();

if(documents.length) main();
function main(){
var Keys = getNamesPlusIDs();
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], 0,XMPConst.PROP_IS_ARRAY);
}
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
if(Keys.length > 0){
    alert("Layer names have been added to keywords");}else{
        alert("No layers to add");
        }
}
function getNamesPlusIDs(){ 
   var ref = new ActionReference(); 
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
   var Names=[];
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) Names.push(layerName);
   }; 
return Names.reverse();
};
 

Top