Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding a function to existing Bridge script

New Here ,
Sep 28, 2017 Sep 28, 2017

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);  

         }  

    } 

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 29, 2017 Sep 29, 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 =

...
Translate
Community Expert ,
Sep 29, 2017 Sep 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).

regex.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2017 Sep 29, 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')

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 30, 2017 Sep 30, 2017

How about...

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2017 Sep 30, 2017

Elegant, the opposite approach! Just deleting the bits that are unnecessary rather than selecting the required bits… I like the use of the alternative pipe, however I think that your regex code should be:

^.+?\s•\s.+\s•\s|\.[^\.]+$

P.S. What is the reasoning for using the global flag SuperMerlin? In a regular expression editor/tester, I can understand how the /g flag works, however in a script it did not seem to matter whether it is used or not?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 30, 2017 Sep 30, 2017

The global flag is there to remove more than one "space fullstop" found, without it, it will only remove the first one found.

Keep up with the good work Stephen.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2017 Sep 30, 2017

Thank you SuperMerlin, as a regex beginner, it is very much a case of baby steps! :]

Unless I am mistaken, it should not matter whether a capture group is used or not:

^.+?\s•\s.+\s•\s|\.[^\.]+$

or

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 30, 2017 Sep 30, 2017

Whatever works is allways good!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 02, 2017 Oct 02, 2017

You guys ARE amazing!

Thank you Stephen. What you did WAS correct and DID work. I didn't give you the absolute correct information, that's my fault. Our file names are more involved than what I posted. That is my error. Here is a more accurate file name:

My Program • Fixture • 9ft • Thingy 1 • Thingy 2 • B • IM.indd

What would need to be displayed in the "Description" field is:

Fixture • 9ft • Thingy 1 • Thingy 2 • B • IM

So when I ran the script, all that showed up was "IM". NOT your fault. My bad information. So I started playing with your statement. I changed it to:

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

I have no IDEA what I did, but it did work in removing the first set of words up to the bullet. If you have time to explain what I removed, that would be great. I honestly have no idea what I did. I apologize, I am trying to learn without any real knowledge of what I am doing.

AWESOME JOB Stephen!

Thank you, Christian

Stephen_A_Marsh  wrote

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')...

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')
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2017 Oct 02, 2017
LATEST

Hi Christian, I have highlighted in red the bits that you removed:

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

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

The explanation for the bits in red are:

. = “Any” character

+ = Quantifier, 1 or more times (greedy)

\s = Any white-space character

= Bullet character

\s = Any white-space character

If you search the web for the term “regular expressions” or “regexp” or “regex” or “grep” you will find out a whole lot more! I can’t script, so I decided that the best use of my limited time and grey-matter was to start learning regex as it is useful in many areas of computing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines