Script to add keywords in Bridge based on the filename
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
