Skip to main content
Known Participant
May 14, 2021
Answered

Bridge: how can i shorten Metadata field to copy it

  • May 14, 2021
  • 1 reply
  • 1130 views

Hello,

i want to copy a metadata field from on field to the other.

abcdef12345

But i want to copy online 12345?

How can i do this?

Thanks, Jörn

This topic has been closed for replies.
Correct answer gregreser

I should have asked what is not working. The script runs and writes data, so I assume the problem is that the resulting value is not correct. I got the entire filename written to SubjectCode when I tested it.

 

If your filenames always have the same format this will not be hard. 

As an example, if your filename is abcdef20000032.jpg

and you only want the subject code "20000032"

you would use this substr:

var Credit = decodeURI(selectedFile.name).substr(6, 8);

 

If your filenames have a variable number of characters or the file extensions are not always 3 characters, then it will be harder, but not too hard.

 


The other problem is that Iptc4xmpCore:SubjectCode is not a langAlt array, it's a bag array, so it was being written incorrectly.

 

I added a different substr that will work if your filenames are different lengths and are formatted [charactersYouDoNotwant][8 character IPTC SubjectCode].[3 character extension] 

#target bridge
if( BridgeTalk.appName == "bridge" ) {
FT = MenuElement.create("command", "Add FileName to IPTC Suject code", "at the end of Tools");
}
FT.onSelect = function () {
AddFilenameToCredit();
}
function AddFilenameToCredit(){
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 str = decodeURI(selectedFile.name);
// works if filename is formatted [charactersYouDoNotwant][8 character IPTC SubjectCode].[3 character extension]  example: abcdef20000032.jpg
var Credit = str.substring(str.length-12, str.length-4);
// works if filename is always the same length
//var Credit = str.substr(6, 8);

var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
myXmp.deleteProperty(XMPConst.NS_IPTC_CORE, "Iptc4xmpCore:SubjectCode");
myXmp.appendArrayItem(XMPConst.NS_IPTC_CORE, "Iptc4xmpCore:SubjectCode", Credit, 0, XMPConst.ARRAY_IS_UNORDERED);
if (myXmpFile.canPutXMP(myXmp)) {
myXmpFile.putXMP(myXmp);
myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}
}
}

 

1 reply

Jörn5F90Author
Known Participant
May 14, 2021

not online, only.

Jörn5F90Author
Known Participant
May 15, 2021

My code, but doesn` t work.

 

#target bridge
if( BridgeTalk.appName == "bridge" ) {
FT = MenuElement.create("command", "Add FileName to IPTC Suject code", "at the end of Tools");
}
FT.onSelect = function () {
AddFilenameToCredit();
}
function AddFilenameToCredit(){
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 Credit = decodeURI(selectedFile.name).substr(length -13, length -4);
var Credit = decodeURI(selectedFile.name).replace(1,2);
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN,
XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
myXmp.deleteProperty(XMPConst.NS_IPTC_CORE, "Iptc4xmpCore:SubjectCode");
myXmp.appendArrayItem(XMPConst.NS_IPTC_CORE, "Iptc4xmpCore:SubjectCode", Credit, 0,
XMPConst.ALIAS_TO_ALT_TEXT);
myXmp.setQualifier(XMPConst.NS_IPTC_CORE, "Iptc4xmpCore:SubjectCode[1]",
"http://www.w3.org/XML/1998/namespace", "lang", "x-default");
if (myXmpFile.canPutXMP(myXmp)) {
myXmpFile.putXMP(myXmp);
myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}
}
}

 

can anyone help

gregreser
Legend
May 15, 2021

@Jörn5F90 can you give us an exampe filename and the desired value you want written to SubjectCode?