Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I’ll try to come back a bit later with the actual script code as I am pressed for time now. There are many valid regular expressions to use in the search/replace code, three examples include:
Search = (^SCRB)(.{2})(.)(_.+)
Replace = Northern Coastal Scrub: Plot $2, Transect $3
or:
Search = (^SCRB)(\d{2})(\w)(_\d+)(\.[^\.]+$)
Replace = Northern Coastal Scrub: Plot $2, Transect $3
or even:
Search = (?:^SCRB)(\d{2})(\w)(?:_\d+)(?:\.[^\.]+$)
Replace = Northern Coastal Scrub: Plot $1, Transect $2
This of course presumes that there is a single leading “fixed” code that equals SCRB and is always replaced with Northern Coastal Scrub and that there are no other leading four letter codes such as ABCD that would introduce another variable for the find/replace routine.
Copy link to clipboard
Copied
Stephen,
Thanks for your reply. Those regular expressions would work perfectly - but we I do have multiple 4 letter codes. In my code where I tried to use a "switch" expression, you'll see I was attempting to change the variable based on the 4 letter code (e.g. SCRB = Northern Coastal Scrub vs. CLOW = Coastal Live Oak Woodlands). We have about 15 different 4 letter codes.
I'm sure this is not the most efficient method and can probably use the regular expressions you noted somehow, but this works for pulling out the different sections and inserting into the Title. Just need to figure out how to translate the codes...
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 CommunityType = decodeURI(selectedFile.name).slice(0,4);
var Plot = decodeURI(selectedFile.name).slice(4,6);
var Transect = decodeURI(selectedFile.name).slice(6,7);
var NewTitle = CommunityType + " Plot " + Plot + ", Transect " + Transect;
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= NewTitle;
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);
}
}
}
Copy link to clipboard
Copied
Ah, I am new to scripting so features like slice or switch are foreign to me… I decided to learn regex first as that seemed the best use of my time as it can be used in scripting and many other places.
EDIT: Perhaps a square peg in a round hole solution, however as my base in regex, I would look for a way to chain multiple regex search/replace commands together.
javascript - String.replace for multiple unique characters and replacements - Stack Overflow
Copy link to clipboard
Copied
It seems to me that code should be fixed ...
so for use we have to use the following code.
O código com meus ajustes para converter o nome do arquivo é:
Copy link to clipboard
Copied
Sarah,
I think you are using the wrong variable name Filename, when it should be FileName, e.g., var CommunityType = FileName.slice(0,4)
I changed these in your code and it works for me now.
#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);
}
}
}
Copy link to clipboard
Copied
Great job gregreser!