Skip to main content
Participant
September 28, 2017
Answered

Adding a function to existing Bridge script

  • September 28, 2017
  • 1 reply
  • 1275 views

Hello everyone.

I am looking to use a metadata field in InDesign as either a "Live Caption" or a "Text Variable". I found this script that will populate the "Description" field with the file name. That's 90% of my battle. What I also need this to do is remove part of the title in the description field for the Caption/Text Variable. Our file name titles are very strict, and different for different programs. To clarify:

My • Program • the file name actually used.indd

Her • Programs • the file name actually used.indd

His • File Name has • the file name actually used.indd

All that I want to show up in the description field is:

the file name actually used

This script does not put .indd into the description, that's good. But I need to be able to put in MULTIPLE beginning variables to be stripped out. E.g.

My • Program •

Her • Programs •

His • File Name has •

I have very little understanding of adding to this script. I would rather not have 2 scripts for one action. If anyone is able to help, I would appreciate it.
Christian

#target bridge    

   if( BridgeTalk.appName == "bridge" ) {   

FT = MenuElement.create("command", "Add File Name to Description", "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, "description"); 

for(var i = 1;i <= count;i++){ 

Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i)); 

    } 

Desc=Desc.toString() + " " + FileName; 

        myXmp.deleteProperty(XMPConst.NS_DC, "description"); 

        myXmp.appendArrayItem(XMPConst.NS_DC, "description", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT); 

        myXmp.setQualifier(XMPConst.NS_DC, "description[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default"); 

        if (myXmpFile.canPutXMP(myXmp)) {  

        myXmpFile.putXMP(myXmp); 

         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);  

         }  

    } 

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

OK, the issue was not in my regex, it was an unnecessary white space character enclosed in double quotes a few lines down. Full working code follows, enjoy!

// https://forums.adobe.com/message/9857999#9857999

#target bridge

   if( BridgeTalk.appName == "bridge" ) {

FT = MenuElement.create("command", "TEST", "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(/(^.+?\s•\s.+\s•\s)(.+)(\.[^\.]+$)/,'$2')

      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, "description");

for(var i = 1;i <= count;i++){

Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));

    }

Desc=Desc.toString() + "" + FileName;

        myXmp.deleteProperty(XMPConst.NS_DC, "description");

        myXmp.appendArrayItem(XMPConst.NS_DC, "description", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT);

        myXmp.setQualifier(XMPConst.NS_DC, "description[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default");

        if (myXmpFile.canPutXMP(myXmp)) {

        myXmpFile.putXMP(myXmp);

         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);

         }

    }

}

P.S. There are many regex’s that are valid, the following may offer slightly better performance on many images as it only uses a single capture group rather than three:

var FileName = decodeURI(selectedFile.name).replace(/(?:^.+?\s•\s.+\s•\s)(.+)(?:\.[^\.]+$)/,'$1')

1 reply

Stephen Marsh
Community Expert
Community Expert
September 29, 2017

You will need to change the Regular Expression in line 11…the bit after “replace”.

The new bit to add would be similar to:

^.+?\s•\s.+\s•\s

(not sure about the bullet characters, you may need to use their ASCII code).

The new combined regex find could be:

(^.+?\s•\s.+\s•\s)(.+)(\.[^\.]+$)

Now we need to incorporate the second capture group $2 into the replace part of the code. So the new line line 11 code would be:

var FileName = decodeURI(selectedFile.name).replace(/(^.+?\s•\s.+\s•\s)(.+)(\.[^\.]+$)/,'$2')

The only problem with my code hack is that it is leaving an unwanted leading white space which I have to work where I went wrong…however the above is 99% there!

This must be due to my pack of knowledge of JavaScript regular expressions, as it appears to work correctly with PCRE regular expressions (the highlighting below in red does not include the white space).

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 30, 2017

OK, the issue was not in my regex, it was an unnecessary white space character enclosed in double quotes a few lines down. Full working code follows, enjoy!

// https://forums.adobe.com/message/9857999#9857999

#target bridge

   if( BridgeTalk.appName == "bridge" ) {

FT = MenuElement.create("command", "TEST", "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(/(^.+?\s•\s.+\s•\s)(.+)(\.[^\.]+$)/,'$2')

      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, "description");

for(var i = 1;i <= count;i++){

Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));

    }

Desc=Desc.toString() + "" + FileName;

        myXmp.deleteProperty(XMPConst.NS_DC, "description");

        myXmp.appendArrayItem(XMPConst.NS_DC, "description", Desc, 0, XMPConst.ALIAS_TO_ALT_TEXT);

        myXmp.setQualifier(XMPConst.NS_DC, "description[1]", "http://www.w3.org/XML/1998/namespace", "lang", "x-default");

        if (myXmpFile.canPutXMP(myXmp)) {

        myXmpFile.putXMP(myXmp);

         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);

         }

    }

}

P.S. There are many regex’s that are valid, the following may offer slightly better performance on many images as it only uses a single capture group rather than three:

var FileName = decodeURI(selectedFile.name).replace(/(?:^.+?\s•\s.+\s•\s)(.+)(?:\.[^\.]+$)/,'$1')

SuperMerlin
Inspiring
September 30, 2017

How about...

var FileName = decodeURI(selectedFile.name).replace(/(\s•)|(\..+$)/g,'');