Skip to main content
Inspiring
July 20, 2020
Answered

Script to add folder name as additional keyword - PROBLEM in keywords are added in quotation marks

  • July 20, 2020
  • 2 replies
  • 1182 views

Can someone help me modify this Bridge script?  I have used scripts to add file and folder names to the metadata description field and I am trying to adapt one of those scripts to add keywords. But when the keywords are added they end up with quotation marks around the result.

"keywords; keywords; keyword3"

 

I think the reason is that I am not adding new keywords as part of an array but as a string. But I'm having trouble correcting that.

 

Note that I do not get quotation marks when I use this line of code: 
Desc=Desc.toString() + "" + FolderName;

And with the above line, the script adds the keyword but without a separation of space or comma

But when I modify the line to add semicolon and space I get the quotation marks: 
Desc=Desc.toString() + "; " + FolderName;

The resulting keywords are like this "apple; testfolder"  (in this case apple was an existing keyword that I want to keep and testfolder is the folder containing the selected files.

 

Here is the code.

 

//this adds the folder name of selected files but not with space as a second keyword
// added semicolon spacein line 33 but it results in  keywords enclosed in quotes
 
 #target bridge     
if( BridgeTalk.appName == "bridge" ) {    
FT = MenuElement.create("command", "FOLDERName to KEYWORD-33", "at the end of Tools");  
}  
FT.onSelect = function () { 
    var thumbs = app.document.selections;   
    if(!thumbs.length) return;  
    if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");  
    for(var a in thumbs)
{  
    var selectedFile = thumbs[a].spec;      
    var FileName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '')  // get file name - not neededfor this script

    var FolderName = (decodeURI(selectedFile.parent.name)); //get foldername  selected File defined above - i added this line

      var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);   
    var myXmp = myXmpFile.getXMP();  
    var Desc=[];  
    var count =  myXmp.countArrayItems(XMPConst.NS_DC, "subject");  //replaced description with subject
    for(var i = 1;i <= count;i++)
 {  
    Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "subject", i));  //replaced description with subject
 }  
    Desc=Desc.toString() + "" + FolderName; //was FileName                                            //adding  semi colon and space to string
        myXmp.deleteProperty(XMPConst.NS_DC, "subject");  //replaced description with subject
        myXmp.appendArrayItem(XMPConst.NS_DC, "subject", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT);  //replaced description with subject
        myXmp.setQualifier(XMPConst.NS_DC, "subject[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default");  //replaced description with subject
        if (myXmpFile.canPutXMP(myXmp)) 
    {   
        myXmpFile.putXMP(myXmp);  
         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);   
       }   
    }  
}  

 

This topic has been closed for replies.
Correct answer Stephen Marsh

You are correct that Keywords need an array, they are not a simple string as with Description. This has caught me out in the past with both Adobe scripting and when using ExifTool. Try this script from the great SuperMerlin:

 

 

/* 
https://forums.adobe.com/thread/2411817
https://community.adobe.com/t5/bridge/adding-image-metadata-from-folder-s-name/td-p/9542797?page=1
*/

#target bridge
   if( BridgeTalk.appName == "bridge" ) {
folderTokeys = MenuElement.create("command", "Add Folder Name to Keywords", "at the end of Tools");
}
folderTokeys.onSelect  = function () {
var thumbs = app.document.selections;
if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var FolderName =  decodeURI(Folder(app.document.presentationPath).name);
for(var a =0;a<thumbs.length;a++){
var selectedFile =  new Thumbnail(thumbs[a]);
app.synchronousMode = true;
var xmp = new XMPMeta(selectedFile.synchronousMetadata.serialize());
xmp.appendArrayItem(XMPConst.NS_DC, "subject", FolderName, 0,XMPConst.PROP_IS_ARRAY);
var newPacket = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
selectedFile.metadata = new Metadata(newPacket);
    }
};

 

 

2 replies

Legend
July 20, 2020

thumbs is an array of selected thumbnails

keys is an array of keywords

 

var md = thumbs[a].synchronousMetadata;
var xmp = new XMPMeta(md.serialize());
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', keys[b], 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem('http://ns.adobe.com/lightroom/1.0/', 'hierarchicalSubject', keys[b], 0, XMPConst.PROP_IS_ARRAY);
var updatedPacket = xmp.serialize(XMPConst.SERIALIZE_OMIT_PACKET_WRAPPER | XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
thumbs[a].metadata = new Metadata(updatedPacket);

Inspiring
July 24, 2020

Thanks for this Lumigraphics!

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 20, 2020

You are correct that Keywords need an array, they are not a simple string as with Description. This has caught me out in the past with both Adobe scripting and when using ExifTool. Try this script from the great SuperMerlin:

 

 

/* 
https://forums.adobe.com/thread/2411817
https://community.adobe.com/t5/bridge/adding-image-metadata-from-folder-s-name/td-p/9542797?page=1
*/

#target bridge
   if( BridgeTalk.appName == "bridge" ) {
folderTokeys = MenuElement.create("command", "Add Folder Name to Keywords", "at the end of Tools");
}
folderTokeys.onSelect  = function () {
var thumbs = app.document.selections;
if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var FolderName =  decodeURI(Folder(app.document.presentationPath).name);
for(var a =0;a<thumbs.length;a++){
var selectedFile =  new Thumbnail(thumbs[a]);
app.synchronousMode = true;
var xmp = new XMPMeta(selectedFile.synchronousMetadata.serialize());
xmp.appendArrayItem(XMPConst.NS_DC, "subject", FolderName, 0,XMPConst.PROP_IS_ARRAY);
var newPacket = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
selectedFile.metadata = new Metadata(newPacket);
    }
};

 

 

Stephen Marsh
Community Expert
Community Expert
July 20, 2020

An equivalent ExifTool command line code would be:

 

exiftool -r '-Subject<${directory;s/.*\/([^\/]*$)/$1/}' 'MAC FILE or FOLDER PATH'

 

exiftool -r "-Subject<${directory;s/.*\/([^\/]*$)/$1/}" "WIN FILE or FOLDER PATH"

 

Adding a + plus sign should append (untested):

 

exiftool -r '-Subject+<${directory;s/.*\/([^\/]*$)/$1/}' 'MAC FILE or FOLDER PATH'

 

exiftool -r "-Subject+<${directory;s/.*\/([^\/]*$)/$1/}" "WIN FILE or FOLDER PATH"