Skip to main content
mBornbach
Known Participant
August 30, 2016
Answered

Script to add keywords in Bridge based on the filename

  • August 30, 2016
  • 2 replies
  • 3508 views

I'm working on a script that adds keywords based on an image's filename. So, if the filename is AG123456(08_16).tif, this script would add "Agriculture", "123456", and "8/16" as keywords in bridge. I've got a small sample of the part that extracts each keyword from the filename written in AppleScript below. I know that I need to write the part that actually adds the keywords in Bridge in JavaScript, because Bridge isn't AppleScript capable. I'm having trouble finding the JavaScript code I need to add the keyword. If anyone could help me out with that, I'd really appreciate it.

I'd like to call the JavaScript from within AppleScript rather than write the entire code in JavaScript. (The full version of this is longer, it would be a lot of code to change to JavaScript).

on open of droppedfiles

  with timeout of 900 seconds -->Changing timeout to 15 minutes. Default is 2 minutes.

  -------------------------------------------------------------

  --GETTING THE CHARACTERS OF THE FILENAME

  -------------------------------------------------------------

  repeat with afile in droppedfiles

  tell application "Finder"

  set fileName to name of afile

  end tell

  -------------------------------------------------------------

  -------------------------------------------------------------

  -------------------------------------------------------------

  --ADDING A KEYWORD FOR EACH PREFIX

  -------------------------------------------------------------

  --Finding the prefix in the filename

  if text 1 thru 2 of fileName contains "AG" then

  set prefixKeyword to "Agriculture"

  else if text 1 thru 2 of fileName contains "AU" then

  set prefixKeyword to "Automotive"

  else

  set prefixKeyword to "Need prefix"

  end if

  --Adding the prefix as a keyword

  display dialog prefixKeyword as string

  -------------------------------------------------------------

  -------------------------------------------------------------

  -------------------------------------------------------------

  --ADDING A KEYWORD FOR EACH SKU NUMBER

  -------------------------------------------------------------

  --Creating a list of all single digit numbers

  set numberList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

  --CHECKING FOR THE SKU NUMBER

  --Checking for a six digit number that starts on the third character of the filename

  if numberList contains (text 3 of fileName) and numberList contains (text 4 of fileName) and numberList contains (text 5 of fileName) and numberList contains (text 6 of fileName) and numberList contains (text 7 of fileName) then

  if numberList does not contain text 9 of fileName then

  if numberList contains (text 8 of fileName) then

  set skuNumber to text 3 thru 8 of fileName

  set skuNumberOffset to 8

  end if

  end if

  --Checking for a six digit number that starts on the fourth character of the filename

  else if numberList contains (text 4 of fileName) and numberList contains (text 5 of fileName) and numberList contains (text 6 of fileName) and numberList contains (text 7 of fileName) and numberList contains (text 8 of fileName) then

  if numberList does not contain text 10 of fileName then

  if numberList contains (text 9 of fileName) then

  set skuNumber to text 4 thru 9 of fileName

  set skuNumberOffset to 9

  end if

  end if

  --Setting the SKU Number Keyword to a warning keyword the user can search for

  else

  set skuNumber to "Need SKU Number"

  set skuNumberOffset to 0

  end if

  --ADDING THE SKU NUMBER AS A KEYWORD

  display dialog skuNumber as string

  -------------------------------------------------------------

  -------------------------------------------------------------

  -------------------------------------------------------------

  --ADDING DATE FROM FILENAME

  -------------------------------------------------------------

  if fileName contains "(" and fileName contains ")" then

  set leftParen to "("

  set rightParen to ")"

  --Getting the month from the filename

  set monthOffset to (offset of leftParen in fileName) + 1 -->returns character right after (

  set fileNameMonth to character monthOffset of fileName

  --Getting all digits of the year from the filename

  set fileNameYearOffset1 to (offset of leftParen in fileName) + 3

  set fileNameYearOffset2 to (offset of rightParen in fileName) - 1

  set fileNameYear1 to character fileNameYearOffset1 of fileName

  set fileNameYear2 to character fileNameYearOffset2 of fileName

  set fileNameYear to characters fileNameYearOffset1 thru fileNameYearOffset2 of fileName

  set dateKeyword to fileNameMonth & "/" & fileNameYear

  --ADDING THE SKU NUMBER AS A KEYWORD

  display dialog dateKeyword as string

  end if

  -------------------------------------------------------------

  -------------------------------------------------------------

  end repeat

  end timeout

end open

This topic has been closed for replies.
Correct answer SuperMerlin

Here is an example of adding keywords to a file, the code can be used in Bridge or Photoshop.

var Keys = ["keyword1","keyword2","keyword3"];

var file = File (Folder.desktop + "/zz.jpg");

if(file.exists) setKeyword( file,Keys);

function setKeyword( file,Keys){

if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

        var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );

        var xmp = xmpf.getXMP();

        for(var s in Keys){

        xmp.appendArrayItem(XMPConst.NS_DC, "subject", Keys, 0,XMPConst.PROP_IS_ARRAY);

    }

      if (xmpf.canPutXMP( xmp )) {

         xmpf.putXMP( xmp );

      }

      xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );

};

2 replies

SuperMerlin
SuperMerlinCorrect answer
Inspiring
August 31, 2016

Here is an example of adding keywords to a file, the code can be used in Bridge or Photoshop.

var Keys = ["keyword1","keyword2","keyword3"];

var file = File (Folder.desktop + "/zz.jpg");

if(file.exists) setKeyword( file,Keys);

function setKeyword( file,Keys){

if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

        var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );

        var xmp = xmpf.getXMP();

        for(var s in Keys){

        xmp.appendArrayItem(XMPConst.NS_DC, "subject", Keys, 0,XMPConst.PROP_IS_ARRAY);

    }

      if (xmpf.canPutXMP( xmp )) {

         xmpf.putXMP( xmp );

      }

      xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );

};

mBornbach
mBornbachAuthor
Known Participant
September 9, 2016

I must have done something wrong as the script doesn't appear to be actually adding any keywords. It isn't giving me any errors either. I added a JavaScript alert to check if the AppleScript variables are successfully being passed to the JavaScript. The alert displays the different keywords I want to add successfully, so that part works. I'm guessing I need to fix the JavaScript at the bottom of the script (inside the handler).  If anyone could help me figure out how to fix that part, I'd greatly appreciate it!


on open of droppedfiles

    with timeout of 900 seconds -->Changing timeout to 15 minutes. Default is 2 minutes.

       

       

        -------------------------------------------------------------

        --GETTING THE CHARACTERS OF THE FILENAME

        -------------------------------------------------------------

       

        repeat with afile in droppedfiles

            tell application "Finder"

                set fileName to name of afile

            end tell

           

            -------------------------------------------------------------

            -------------------------------------------------------------

           

           

           

           

            -------------------------------------------------------------

            --ADDING A KEYWORD FOR EACH PREFIX

            -------------------------------------------------------------

           

            --Finding the prefix in the filename

            if text 1 thru 2 of fileName contains "AG" then

                set prefixKeyword to "Agriculture"

               

            else if text 1 thru 2 of fileName contains "AU" then

                set prefixKeyword to "Automotive"

               

            else if text 1 thru 2 of fileName contains "BI" then

                set prefixKeyword to "Bicycle"

               

            else if text 1 thru 2 of fileName contains "SG" then

                set prefixKeyword to "Sporting Goods"

               

            else if text 1 thru 2 of fileName contains "TY" then

                set prefixKeyword to "Toy"

               

            else

                set prefixKeyword to "Need prefix"

               

            end if

           

           

            --Calling the addKeyword handler at the bottom of the script to add the prefix as a keyword

            my addKeyword(afile, prefixKeyword)

           

            -------------------------------------------------------------

            -------------------------------------------------------------

           

           

           

           

            -------------------------------------------------------------

            --ADDING A KEYWORD FOR EACH SKU NUMBER

            -------------------------------------------------------------

           

            --Creating a list of all single digit numbers

            set numberList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

           

            --CHECKING FOR THE SKU NUMBER

            --Checking for a six digit number that starts on the third character of the filename

            if numberList contains (text 3 of fileName) and numberList contains (text 4 of fileName) and numberList contains (text 5 of fileName) and numberList contains (text 6 of fileName) and numberList contains (text 7 of fileName) then

                if numberList does not contain text 9 of fileName then

                    if numberList contains (text 8 of fileName) then

                        set skuNumber to text 3 thru 8 of fileName

                        set skuNumberOffset to 8

                    end if

                end if

               

                --Checking for a six digit number that starts on the fourth character of the filename

            else if numberList contains (text 4 of fileName) and numberList contains (text 5 of fileName) and numberList contains (text 6 of fileName) and numberList contains (text 7 of fileName) and numberList contains (text 8 of fileName) then

                if numberList does not contain text 10 of fileName then

                    if numberList contains (text 9 of fileName) then

                        set skuNumber to text 4 thru 9 of fileName

                        set skuNumberOffset to 9

                    end if

                end if

               

               

                --Setting the SKU Number Keyword to a warning keyword the user can search for if no SKU number is found   

            else

                set skuNumber to "Need SKU Number"

                set skuNumberOffset to 0

            end if

           

           

           

            --Calling the addKeyword handler at the bottom of the script to add the SKU number as a keyword

            my addKeyword(afile, skuNumber)

           

           

            -------------------------------------------------------------

            -------------------------------------------------------------

           

           

           

           

            -------------------------------------------------------------

            --ADDING DATE FROM FILENAME

            -------------------------------------------------------------

           

            if fileName contains "(" and fileName contains ")" then

                set leftParen to "("

                set rightParen to ")"

               

                --Getting the month from the filename

                set monthOffset to (offset of leftParen in fileName) + 1 -->returns character right after (

                set fileNameMonth to character monthOffset of fileName

               

                --Getting all digits of the year from the filename

                set fileNameYearOffset1 to (offset of leftParen in fileName) + 3

                set fileNameYearOffset2 to (offset of rightParen in fileName) - 1

                set fileNameYear1 to character fileNameYearOffset1 of fileName

                set fileNameYear2 to character fileNameYearOffset2 of fileName

                set fileNameYear to characters fileNameYearOffset1 thru fileNameYearOffset2 of fileName

               

                set dateKeyword to fileNameMonth & "/" & fileNameYear

               

               

                --Calling the addKeyword handler at the bottom of the script to add the date as a keyword

                my addKeyword(afile, dateKeyword)

               

            end if

           

           

        end repeat

    end timeout

end open

-------------------------------------------------------------

-------------------------------------------------------------

-------------------------------------------------------------

-------------------------------------------------------------           

--HANDLER TO ADD KEYWORDS

on addKeyword(myFile, myKeyword)

    tell application "Adobe Bridge CS6"

        activate

       

        do javascript "

var Keys = '" & myKeyword & "';

var file = '" & myFile & "'; 

if(file.exists) setKeyword(file, Keys);

function setKeyword(Keys){ 

if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript'); 

        var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE ); 

        var xmp = xmpf.getXMP(); 

        for(var s in Keys){ 

        xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys, 0,XMPConst.PROP_IS_ARRAY); 

    } 

      if (xmpf.canPutXMP( xmp )) { 

         xmpf.putXMP( xmp ); 

      } 

      xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY ); 

}; 

//Displaying an alert to check if it's passing in the AppleScript variables

        alert(Keys);

  /**/"

       

       

    end tell

end addKeyword

SuperMerlin
Inspiring
September 9, 2016

You are not passing the filename to the function as you have:-

function setKeyword(Keys){

and should be

function setKeyword(file,Keys){

Stephen Marsh
Community Expert
August 31, 2016

Does this have to be a Bridge script? What if this could be performed at the OS level? Mac only or Win too?

To try to answer your original request, try this topic thread:

https://forums.adobe.com/thread/656144