Adding Keywords in Bridge

I’m attempting to add keywords to images in Bridge using the following script. Unfortunately, the only AppleScript command Bridge understands is “do JavaScript” So, I have to write the part that actually adds keywords in JavaScript.

I added a JavaScript alert at the end of the JavaScript part to test if the AppleScript variables are successfully being passed to JavaScript. That part works. The event replies in the AppleScript editor show that it is also trying to work with the correct file. It also doesn’t give me any errors. Still, the keywords are not being added in Bridge. I’m guessing something needs to change in the JavaScript part at the bottom of the code, but I’ve had very minimal experience with JavaScript.
Any help would be greatly appreciated. :slight_smile:

with timeout of 900 seconds

	-------------------------------------------------------------
	--GETTING THE CHARACTERS OF THE FILENAME
	-------------------------------------------------------------

	tell application "Finder" to set myFolder to choose folder with prompt "Select folder"
	tell application "Finder" to set myFiles to (every file of folder myFolder whose name extension contains "tif") as alias list
	repeat with afile in myFiles
		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
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------			

--HANDLER TO ADD KEYWORDS
on addKeyword(myFile, myKeyword)
	tell application "Adobe Bridge CC"
		activate
		
		do javascript "
var Keys = '" & myKeyword & "';

var file = '" & myFile & "';  
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[s], 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




Browser: Firefox 47.0
Operating System: Mac OS X (10.10)