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

Script for converting filename and entering into metadata title field

New Here ,
Dec 12, 2018 Dec 12, 2018

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

TOPICS
Scripting

Views

1.3K

Translate

Translate

Report

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 ,
Dec 12, 2018 Dec 12, 2018

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.

Votes

Translate

Translate

Report

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 ,
Dec 12, 2018 Dec 12, 2018

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

         }  

    } 

Votes

Translate

Translate

Report

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 ,
Dec 12, 2018 Dec 12, 2018

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

Votes

Translate

Translate

Report

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 Beginner ,
Dec 12, 2018 Dec 12, 2018

Copy link to clipboard

Copied

It seems to me that code should be fixed ...

so for use we have to use the following code.

  1. #target bridge      
  2.    if (BridgeTalk.appName ==  "ponte" ) {     
  3. FT = MenuElement.create ( "comando" "Adicionar nome do arquivo ao título" "no final das ferramentas" );   
  4. }   
  5. FT.onSelect = function () {    
  6. var thumbs = app.document.selections;    
  7. if (! thumbs.length)  retornar ;   
  8. if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript =  novo ExternalObject ( "lib: AdobeXMPScript" );   
  9. para (var a in thumbs) {   
  10. var selectedFile = thumbs .spec;       
  11. var FileName = decodeURI (selectedFile.name) .replace (/\.[^\.]+$/,  '' )   
  12.       var myXmpFile =  new XMPFile (selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);    
  13.   var myXmp = myXmpFile.getXMP ();   
  14. var Desc = =];   
  15. var count = myXmp.countArrayItems (XMPConst.NS_DC,  "title" );   
  16. para (var i =  1 ; i <= count; i ++) {   
  17. Desc.push (myXmp.getArrayItem (XMPConst.NS_DC,  "title" , i));   
  18.     }   
  19. Desc = Desc.toString () +  "" + FileName;   
  20.         myXmp.deleteProperty (XMPConst.NS_DC,  "title" );   
  21.         myXmp.appendArrayItem (XMPConst.NS_DC,  "title" , Desc,  0 , XMPConst.ALIAS_TO_ALT_TEXT);   
  22.         myXmp.setQualifier (XMPConst.NS_DC,  "title [1]" "http://www.w3.org/XML/1998/namespace" "idioma" "x-default" );   
  23.         if (myXmpFile.canPutXMP (myXmp)) {    
  24.         myXmpFile.putXMP (myXmp);   
  25.          myXmpFile.closeFile (XMPConst.CLOSE_UPDATE_SAFELY);    
  26.          }    
  27.     }   
  28. }   

O código com meus ajustes para converter o nome do arquivo é:

  1. #target bridge      
  2.    if (BridgeTalk.appName ==  "ponte" ) {     
  3. FT = MenuElement.create ( "comando" "Converter nome de arquivo para título" "no final de ferramentas" );   
  4. }   
  5. FT.onSelect = function () {    
  6. var thumbs = app.document.selections;    
  7. if (! thumbs.length)  retornar ;   
  8. if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript =  novo ExternalObject ( "lib: AdobeXMPScript" );   
  9. para (var a in thumbs) {   
  10. var selectedFile = thumbs .spec;       
  11. var FileName = decodeURI (selectedFile.name) .slice ( 0 , 7 )  
  12.       var myXmpFile =  new XMPFile (selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);    
  13.   var myXmp = myXmpFile.getXMP ();   
  14. var CommunityType = Filename.slice ( 0 , 4
  15.      switch (CommunityType) {  
  16.           case 'SCRB' :  
  17.                CommunityType =  "Esfrega Costeira do Norte:"
  18.                pausa
  19.           caso 'CLOW'
  20.                CommunityType =  "Coast Live Oak Woodlands:" 
  21.      } 
  22. var Plot = Filename.slice ( 5 , 6
  23. var Transect = Filename.slice ( 7
  24. var NewFileName = CommunityType +  "Plot" + Plot +  ", Transect" + Transect 
  25.   var Desc = =];   
  26. var count = myXmp.countArrayItems (XMPConst.NS_DC,  "title" );   
  27. para (var i =  1 ; i <= count; i ++) {   
  28. Desc.push (myXmp.getArrayItem (XMPConst.NS_DC,  "title" , i));   
  29.     }   
  30. Desc = Desc.toString () +  "" + NewFileName;   
  31.         myXmp.deleteProperty (XMPConst.NS_DC,  "title" );   
  32.         myXmp.appendArrayItem (XMPConst.NS_DC,  "title" , Desc,  0 , XMPConst.ALIAS_TO_ALT_TEXT);   
  33.         myXmp.setQualifier (XMPConst.NS_DC,  "title [1]" "http://www.w3.org/XML/1998/namespace" "idioma" "x-default" );   
  34.         if (myXmpFile.canPutXMP (myXmp)) {    
  35.         myXmpFile.putXMP (myXmp);   
  36.          myXmpFile.closeFile (XMPConst.CLOSE_UPDATE_SAFELY);    
  37.          }    
  38.     }   
  39. }

Votes

Translate

Translate

Report

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 ,
Jan 21, 2019 Jan 21, 2019

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

        } 

    }

}

Votes

Translate

Translate

Report

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 ,
Jan 21, 2019 Jan 21, 2019

Copy link to clipboard

Copied

LATEST

Great job gregreser!

Votes

Translate

Translate

Report

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