Script for converting filename and entering into metadata title field
Hi,
I'm trying to tweak some code I found for inserting a filename into the metadata title field. What I want it to do is to take the filename which is a code that I can break apart and convert into a more descriptive title. For example if the filename is SCRB01A_20150709.JPG, I want to pull out the first 7 characters which translates to SCRB = Northern Coastal Scrub, 01 = Plot 01, and A = Transect A so the title would be "Northern Coastal Scrub: Plot 01, Transect A".
The code I started with that just puts the filename into the title is:
#target bridge
if( BridgeTalk.appName == "bridge" ) {
FT = MenuElement.create("command", "Add FileName to Title", "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.spec;
var FileName = decodeURI(selectedFile.name).replace(/\.[^\.]+$/, '')
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, "title");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "title", i));
}
Desc=Desc.toString() + " " + FileName;
myXmp.deleteProperty(XMPConst.NS_DC, "title");
myXmp.appendArrayItem(XMPConst.NS_DC, "title", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT);
myXmp.setQualifier(XMPConst.NS_DC, "title[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default");
if (myXmpFile.canPutXMP(myXmp)) {
myXmpFile.putXMP(myXmp);
myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}
}
}
The code with my tweaks to convert the filename is:
#target bridge
if( BridgeTalk.appName == "bridge" ) {
FT = MenuElement.create("command", "Convert FileName to Title", "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.spec;
var FileName = decodeURI(selectedFile.name).slice(0,7)
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var CommunityType = Filename.slice(0,4)
switch (CommunityType) {
case 'SCRB':
CommunityType = "Northern Coastal Scrub:";
break;
case 'CLOW':
CommunityType = "Coast Live Oak Woodlands:"
}
var Plot = Filename.slice(5,6)
var Transect = Filename.slice(7)
var NewFileName = CommunityType + " Plot " + Plot + ", Transect " + Transect
var Desc=[];
var count = myXmp.countArrayItems(XMPConst.NS_DC, "title");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "title", i));
}
Desc=Desc.toString() + " " + NewFileName;
myXmp.deleteProperty(XMPConst.NS_DC, "title");
myXmp.appendArrayItem(XMPConst.NS_DC, "title", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT);
myXmp.setQualifier(XMPConst.NS_DC, "title[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default");
if (myXmpFile.canPutXMP(myXmp)) {
myXmpFile.putXMP(myXmp);
myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}
}
}
The first code that just puts the filename into the Title works. My tweaked code does not. I'm totally new to javascript so not totally sure what I'm doing. Any help would be greatly appreciated!
Thanks,
Sarah
