Copy link to clipboard
Copied
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
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();
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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 );
};
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You are not passing the filename to the function as you have:-
function setKeyword(Keys){
and should be
function setKeyword(file,Keys){
Copy link to clipboard
Copied
Thanks. I changed that line and it doesn't appear to be adding the keywords. The event replies from the AppleScript editor show that it's setting var file to the right file and it's setting var Keys to the right keywords. But, when I open the file up in Bridge, no keywords are showing up in the metadata or keywords tabs.
Here's the event replies I see when the script is processing in the AppleScript editor:
tell application "Adobe Bridge CC"
activate
do javascript "
var Keys = 'Sporting Goods';
var file = 'My HD:Users:mbornbach:Desktop:Testing Script:SG835844(7_16)_s_4c.tif';
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 );
};
//Displaying an alert to check if it's passing in the AppleScript variables
alert(Keys);
/**/"
Here's the current script:
with timeout of 900 seconds
tell application "Finder" to set myFolder to choose folder with prompt "Select folder"
-------------------------------------------------------------
--GETTING THE CHARACTERS OF THE FILENAME
-------------------------------------------------------------
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, 'keywords', 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
Copy link to clipboard
Copied
I also experimented with changing the line from
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys, 0,XMPConst.PROP_IS_ARRAY);
to:
xmp.appendArrayItem(XMPConst.NS_DC, 'keywords', Keys, 0,XMPConst.PROP_IS_ARRAY);
and back
Neither one seems to make a difference
Thanks for all your help!
Copy link to clipboard
Copied
I know nothing about appleScript, I wonder if the javaScript does not like the array that it is passed?
You could try passing a string with keywords seperated by a comma, then the string can be converted to an array in the javascript function.
IE: Keys = "Keyword1,Keyword2,etc";
Then the first line of the function would be to change this to an array...
Keys=Keys.split(',');
This should then be in the correct format for an array.
Worth a try
Copy link to clipboard
Copied
Bridge can confuse the issue due to using a cache that may not update and require purging. Use File/File Info or other software to verify that the keywords exist.
Copy link to clipboard
Copied
I tested the JavaScript only version in the Extend Script editor and it worked perfectly. I noticed I didn't have the word File after "var file =" in the script I had in the AppleScript editor. I fixed that and the script now gets that the file does exist.
Unfortunately, the following script still isn't adding the keywords or giving useful error messages. (I tested it by going to file info in Bridge and by opening the file up in Photoshop and going to file info there). I did get manage to get a script that adds keywords in Photoshop working, but we're going to be adding keywords to files that aren't images, so I'm trying to get something working in Bridge for pdfs, InDesign files, etc.. Here's the pared down version of the script for testing purposes:
set myFile to POSIX path of "Users:melissabornbach:Desktop:WOOD PLANS.pdf"
tell application "Adobe Bridge CS6"
do javascript "
var Keys = 'Keyword1';
var file = File ('" & myFile & "');
if(file.exists){
setKeyword(file, Keys);} else {
alert('no, file does not exist')
}
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 );
};
/**/"
end tell
Copy link to clipboard
Copied
To my great shock, it is now working! I think SuperMerlin was right about it not liking the way the keyword array. Right now, I just have it adding one keyword that is stored in an AppleScript variable. I haven't figured out the correct syntax for adding multiple keywords, but I'm sure I'll get it when it's not so late.
Here's what I have that is working:
set myFile to POSIX path of "Users:melissabornbach:Desktop:WOODPLANS.pdf"
set prefixKeyword to "Kid_Clothing"
set skuNumber to "123456"
set dateKeyword to "August 2016"
set myJavaScript to "
#target 'bridge'
var Keys = ['" & skuNumber & "'];
var file = File ('" & myFile & "');
if(file.exists){
setKeyword(file, Keys);} else {
alert('no, file does not exist')
}
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 );
};
/**/"
tell application "Adobe Bridge CS6"
do javascript myJavaScript
end tell
Copy link to clipboard
Copied
So, I have the script that adds keywords to images working great. Now, I need to add keywords to the ads/publications themselves. I have a script that does add keywords successfully for a few files, but for most of the files, it can't seem to put the xmp. (It displays the alert I added). Any ideas what I might be doing wrong or what Bridge might not like about most of the pdf files I'm trying to add keywords to?
on open of droppedfiles
with timeout of 1500 seconds
repeat with afile in droppedfiles
---------------------------------------------------------------------
--GETTING THE FILENAME, PATH & EXTENSION OF EACH FILE
---------------------------------------------------------------------
tell application "Finder"
set fileName to name of afile
set filePath to container of afile as string
set fileType to name extension of afile
--Getting the name of just the parent folder by chopping all the folders above it off the path
set old_delims to AppleScript's text item delimiters
set delimitName to ":" & fileName as string
set AppleScript's text item delimiters to delimitName
set folder_part1 to first text item of filePath as string
-->returns nas/Advertising Department/Advertising Department Print/Publications/2016 Publications/7012 Spring Projects
set AppleScript's text item delimiters to ":"
set folder_part2 to every text item of folder_part1
-->returns nas/Advertising Department/Advertising Department Print/Publications/2016 Publications/7012 Spring Projects
set parent_folder to item -2 of folder_part2
-->returns 7012 Spring Projects
set parents_parent_folder to item -3 of folder_part2
set AppleScript's text item delimiters to old_delims
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE FILENAME AS A KEYWORD
---------------------------------------------------------------------
--Getting folder keyword
if parent_folder does not contain "PDF" then
set publication_keyword to parent_folder
else if parents_parent_folder does not contain "PDF" then
set publication_keyword to parents_parent_folder
else
set publication_keyword to "Need Input Stephanie"
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE TYPE OF FILE AS A KEYWORD
---------------------------------------------------------------------
if fileType contains "indd" then
set file_type_keyword to "Adobe InDesign Document"
else if fileType contains "qxd" or fileType contains "qxp" then
set file_type_keyword to "QuarkXPress Document"
else if fileType contains "pdf" then
set file_type_keyword to "Adobe Acrobat PDF Document"
else if fileType contains "psd" then
set file_type_keyword to "Photoshop PSD Image"
else if fileType contains "tiff" or fileType contains "tif" then
set file_type_keyword to "Photoshop TIFF Image"
else if fileType contains "jpg" then
set file_type_keyword to "Photoshop JPG Image"
else if fileType contains "png" then
set file_type_keyword to "Photoshop PNG Image"
else if fileType contains "eps" or fileType contains "ai" then
set file_type_keyword to "Adobe Illustrator Vector Graphic"
else if fileType contains "ppt" or fileType contains "pptx" then
set file_type_keyword to "Microsoft Powerpoint Presentation"
else if fileType contains "rtf" or fileType contains "doc" or fileType contains "txt" then
set file_type_keyword to "Text Document"
else if fileType contains "xls" or fileType contains "xlsx" then
set file_type_keyword to "Microsoft Excel Spreadsheet"
else if fileType is "" then
set file_type_keyword to "Need Input Stephanie"
else
set file_type_keyword to "Need Input Stephanie"
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--GETTING THE NUMBER KEYWORD
---------------------------------------------------------------------
--Setting the page number keyword to the first two characters of the filename
--if the first 2 characters are the page number
set twoDigitNumber to {"01", "02", "03", "04", "05", "06", "07", "08", "09", ¬
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", ¬
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", ¬
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", ¬
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", ¬
"65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", ¬
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", ¬
"93", "94", "95", "96", "97", "98", "99"}
if twoDigitNumber contains (text 1 thru 2 of fileName) then
set page_number_keyword to (text 1 thru 2 of fileName)
--Figuring out what the page number should be based
--off the naming system when we pdf the file
else
set singleDigitNumber to {"2", "3", "4", "5", "6", "7", "8", "9"}
set doubleDigitNumber to {"02", "03", "04", "05", "06", "07", "08", ¬
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ¬
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", ¬
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", ¬
"45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", ¬
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", ¬
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", ¬
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", ¬
"93", "94", "95", "96", "97", "98", "99"}
set fileName to name of afile as string --> getting all the characters in the filename
if doubleDigitNumber contains text 1 thru 2 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set a to text 1 thru 2 of fileName as string
else if singleDigitNumber contains text 1 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set a to "0" & text 1 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and doubleDigitNumber contains text 3 thru 4 of fileName then
set a to text 3 thru 4 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and doubleDigitNumber contains text 4 thru 5 of fileName then
set a to text 4 thru 5 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and singleDigitNumber contains text 3 of fileName then
set a to "0" & text 3 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and singleDigitNumber contains text 4 of fileName then
set a to "0" & text 4 of fileName
end if
if doubleDigitNumber contains text 3 thru 4 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to text 3 thru 4 of fileName
else if doubleDigitNumber contains text 4 thru 5 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to text 4 thru 5 of fileName
else if singleDigitNumber contains text 3 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to "0" & text 3 of fileName
else if singleDigitNumber contains text 4 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to "0" & text 4 of fileName
else if text 1 thru 2 of fileName contains "1-" then
set b to "0" & text 1 of fileName
end if
if fileName contains "Page 01" or fileName contains ¬
"Page 1" and text 1 thru 2 of fileName does not contain "1-" then
set pageNumber to a
else if fileName contains "Page 02" or fileName contains ¬
"Page 2" and text 1 thru 2 of fileName does not contain "1-" then
set pageNumber to b
end if
if fileName contains "Page 1" or fileName contains ¬
"Page 01" and text 1 thru 2 of fileName contains "1-" then --contains "1-" then
set pageNumber to a
else if text 1 thru 2 of fileName contains ¬
"1-" and fileName contains "Page 2" or fileName contains "Page 02" then
set pageNumber to b
end if
--Setting the figured out page number to a keyword if possible
try
set page_number_keyword to pageNumber
on error
set page_number_keyword to "Need Input Stephanie"
end try
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE CREATIION DATE OF THE FILE AS A KEYWORD
---------------------------------------------------------------------
try
set date_keyword to the creation date of afile as string
on error
set date_keyword to "Need Input Stephanie"
end try
end tell
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
-------------------------- ADDING THE KEYWORDS IN BRIDGE ----------------------------
---------------------------------------------------------------------------------------------------
--Using JavaScript to add the keywords in Bridge
tell application "Adobe Bridge CC"
do javascript "
#target 'bridge'
//Declaring the six regular keywords
var Keys = ['" & publication_keyword & "'];
var Keys2 = ['" & file_type_keyword & "'];
var Keys3 = ['" & page_number_keyword & "'];
var Keys4 = ['" & date_keyword & "'];
//Telling Bridge which file to add the keywords to
var file = File ('" & afile & "');
//Running the add keyword function only if the file exists
if(file.exists) setKeyword(file, Keys);
//Creating a new XMP metadata file or adding to the existing one
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();
//Adding the first keyword
for(var s in Keys){
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys, 0,XMPConst.PROP_IS_ARRAY);
}
//Adding the second keyword
for(var s in Keys2){
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys2, 0,XMPConst.PROP_IS_ARRAY);
}
//Adding the third keyword
for(var s in Keys3){
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys3, 0,XMPConst.PROP_IS_ARRAY);
}
//Adding the fourth keyword
for(var s in Keys4){
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys4, 0,XMPConst.PROP_IS_ARRAY);
}
//Updating and closing the XMP metadata file
if (xmpf.canPutXMP( xmp )) {
xmpf.putXMP( xmp );
} else {
alert('unable to put xmp, sorry')
}
xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
};
/**/"
end tell
end repeat
---------------------------------------------------------------------
--------------------------------------------------------------------
---------------------------------------------------------------------
--RESETTING USER INTERACTION DIALOGS IN INDESIGN
---------------------------------------------------------------------
tell application "Adobe InDesign CC 2014" to set user interaction level of script preferences to interact with all
---------------------------------------------------------------------
--------------------------------------------------------------------
end timeout
end open
Copy link to clipboard
Copied
The JavaScript is incorrect. You are calling the function :-
if(file.exists) setKeyword(file, Keys);
file being the file itself, Keys is an array of all the keywords so Keys2, Keys3 and Keys4 it does not know about.
You could pass them as a strings I.E.
if(file.exists) setKeyword(file, Key1,Key2,Key3,Key4);
Then :
for(var s in Keys){
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Keys, 0,XMPConst.PROP_IS_ARRAY);
}
Would become:
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key1, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key2, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key3, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key4, 0,XMPConst.PROP_IS_ARRAY);
Copy link to clipboard
Copied
Thanks! Your help with the JavaScript has been invaluable.
I fixed the JavaScript and it still wouldn't cooperate with most of the pdfs, so I realized there must be something it doesn't like about the pdf files. I tried opening a pdf file in Acrobat and saving it. The script worked with the resaved pdf. I added a part to the script that opens each pdf in acrobat, saves and closes it. Now it works great. Here's the full script for anyone else who might be searching for something similar.
on open of droppedfiles
with timeout of 1500 seconds
repeat with afile in droppedfiles
---------------------------------------------------------------------
--GETTING THE FILENAME, PATH & EXTENSION OF EACH FILE
---------------------------------------------------------------------
tell application "Finder"
set fileName to name of afile
set filePath to container of afile as string
set fileType to name extension of afile
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE PARENT FOLDER AS A KEYWORD
---------------------------------------------------------------------
--Getting the name of just the parent folder by chopping all the folders above it off the path
set old_delims to AppleScript's text item delimiters
set delimitName to ":" & fileName as string
set AppleScript's text item delimiters to delimitName
set folder_part1 to first text item of filePath as string
-->returns nas/Advertising Department/Advertising Department Print/Publications/2016 Publications/7012 Spring Projects
set AppleScript's text item delimiters to ":"
set folder_part2 to every text item of folder_part1
-->returns nas/Advertising Department/Advertising Department Print/Publications/2016 Publications/7012 Spring Projects
set parent_folder to item -2 of folder_part2
-->returns 7012 Spring Projects
set parents_parent_folder to item -3 of folder_part2
--> using the folder that contains the parent folder if the parent folder is named PDF
set AppleScript's text item delimiters to old_delims
--Selecting whether to store the parent folder or the parent's parent folder as a keyword
if parent_folder does not contain "PDF" then
set publication_keyword to parent_folder
else if parents_parent_folder does not contain "PDF" then
set publication_keyword to parents_parent_folder
else
set publication_keyword to "Need Input Stephanie"
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE TYPE OF FILE AS A KEYWORD
---------------------------------------------------------------------
if fileType contains "indd" then
set file_type_keyword to "Adobe InDesign Document"
else if fileType contains "qxd" or fileType contains "qxp" then
set file_type_keyword to "QuarkXPress Document"
else if fileType contains "pdf" then
set file_type_keyword to "Adobe Acrobat PDF Document"
else if fileType contains "psd" then
set file_type_keyword to "Photoshop PSD Image"
else if fileType contains "tiff" or fileType contains "tif" then
set file_type_keyword to "Photoshop TIFF Image"
else if fileType contains "jpg" then
set file_type_keyword to "Photoshop JPG Image"
else if fileType contains "png" then
set file_type_keyword to "Photoshop PNG Image"
else if fileType contains "eps" or fileType contains "ai" then
set file_type_keyword to "Adobe Illustrator Vector Graphic"
else if fileType contains "ppt" or fileType contains "pptx" then
set file_type_keyword to "Microsoft Powerpoint Presentation"
else if fileType contains "rtf" or fileType contains "doc" or fileType contains "txt" then
set file_type_keyword to "Text Document"
else if fileType contains "xls" or fileType contains "xlsx" then
set file_type_keyword to "Microsoft Excel Spreadsheet"
else if fileType is "" then
set file_type_keyword to "Need Input Stephanie"
else
set file_type_keyword to "Need Input Stephanie"
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--GETTING THE NUMBER KEYWORD
---------------------------------------------------------------------
--Setting the page number keyword to the first two characters of the filename
--if the first 2 characters are the page number
set twoDigitNumber to {"01", "02", "03", "04", "05", "06", "07", "08", "09", ¬
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", ¬
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", ¬
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", ¬
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", ¬
"65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", ¬
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", ¬
"93", "94", "95", "96", "97", "98", "99"}
if twoDigitNumber contains (text 1 thru 2 of fileName) then
set page_number_keyword to "Page " & (text 1 thru 2 of fileName)
--Figuring out what the page number should be based
--off the naming system when we pdf the file
else
set singleDigitNumber to {"2", "3", "4", "5", "6", "7", "8", "9"}
set doubleDigitNumber to {"02", "03", "04", "05", "06", "07", "08", ¬
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ¬
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", ¬
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", ¬
"45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", ¬
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", ¬
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", ¬
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", ¬
"93", "94", "95", "96", "97", "98", "99"}
set fileName to name of afile as string --> getting all the characters in the filename
if doubleDigitNumber contains text 1 thru 2 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set a to text 1 thru 2 of fileName as string
else if singleDigitNumber contains text 1 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set a to "0" & text 1 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and doubleDigitNumber contains text 3 thru 4 of fileName then
set a to text 3 thru 4 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and doubleDigitNumber contains text 4 thru 5 of fileName then
set a to text 4 thru 5 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and singleDigitNumber contains text 3 of fileName then
set a to "0" & text 3 of fileName
else if text 1 thru 2 of fileName contains ¬
"1-" and singleDigitNumber contains text 4 of fileName then
set a to "0" & text 4 of fileName
end if
if doubleDigitNumber contains text 3 thru 4 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to text 3 thru 4 of fileName
else if doubleDigitNumber contains text 4 thru 5 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to text 4 thru 5 of fileName
else if singleDigitNumber contains text 3 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to "0" & text 3 of fileName
else if singleDigitNumber contains text 4 of fileName ¬
and text 1 thru 2 of fileName does not contain "1-" then
set b to "0" & text 4 of fileName
else if text 1 thru 2 of fileName contains "1-" then
set b to "0" & text 1 of fileName
end if
if fileName contains "Page 01" or fileName contains ¬
"Page 1" and text 1 thru 2 of fileName does not contain "1-" then
set pageNumber to a
else if fileName contains "Page 02" or fileName contains ¬
"Page 2" and text 1 thru 2 of fileName does not contain "1-" then
set pageNumber to b
end if
if fileName contains "Page 1" or fileName contains ¬
"Page 01" and text 1 thru 2 of fileName contains "1-" then --contains "1-" then
set pageNumber to a
else if text 1 thru 2 of fileName contains ¬
"1-" and fileName contains "Page 2" or fileName contains "Page 02" then
set pageNumber to b
end if
--Setting the figured out page number to a keyword if possible
try
set page_number_keyword to "Page " & pageNumber
on error
set page_number_keyword to "Need Input Stephanie"
end try
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--STORING THE CREATION DATE OF THE FILE AS A KEYWORD
---------------------------------------------------------------------
try
set date_keyword to the creation date of afile as string
on error
set date_keyword to "Need Input Stephanie"
end try
end tell
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--BACKING THE FILE UP, OPENING IT IN ACROBAT & SAVING THE FILE
---------------------------------------------------------------------
--Setting the desktop folder
set desktopFolder to path to desktop as string
--Creating the PDFs folder on the desktop if it doesn't exist yet
if not my CheckForFolder(desktopFolder & "PDFs:") then
tell application "Finder"
make new folder at folder desktopFolder with properties {name:"PDFs"}
end tell
end if
set PDFsFolder to desktopFolder & "PDFs" & ":" as string
--Duplicating each file to the PDFs folder on the desktop
tell application "Finder" to duplicate afile to alias PDFsFolder
--Opening the file in Acrobat and saving it, if the file is a pdf
if fileType contains "pdf" then
--Opening the file in Acrobat and saving it
tell application "Adobe Acrobat Pro"
open afile as alias
set savePath to filePath & fileName as string -->The location where the file will be saved
--Saving the file
try
tell document 1
save to file savePath
end tell
on error
tell application "Finder"
set saveFile to savePath as alias
delete saveFile
end tell
tell document 1
save to file savePath
end tell
end try
tell document 1 to close
--Setting the newly saved file to an alias so it can be used by Bridge
set myFile to savePath as alias
end tell
end if
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
--ADDING THE KEYWORDS IN BRIDGE
---------------------------------------------------------------------
--Using JavaScript to add the keywords in Bridge
tell application "Adobe Bridge CC"
do javascript "
#target 'bridge'
//Declaring the keyword variables
var Key1 = ['" & publication_keyword & "'];
var Key2 = ['" & file_type_keyword & "'];
var Key3 = ['" & page_number_keyword & "'];
var Key4 = ['" & date_keyword & "'];
//Telling Bridge which file to add the keywords to
var file = File ('" & myFile & "');
//Running the add keyword function only if the file exists
if(file.exists) setKeyword(file, Key1,Key2,Key3,Key4);
//Creating a new XMP metadata file or adding to the existing one
function setKeyword(file, Key1,Key2,Key3,Key4){
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();
//Adding the keywords
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key1, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key2, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key3, 0,XMPConst.PROP_IS_ARRAY);
xmp.appendArrayItem(XMPConst.NS_DC, 'subject', Key4, 0,XMPConst.PROP_IS_ARRAY);
//Updating and closing the XMP metadata file
if (xmpf.canPutXMP( xmp )) {
xmpf.putXMP( xmp );
} else {
//doing nothing right now, leaving in in case I add a condition to the else statement
}
xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
};
/**/"
end tell
end repeat
end timeout
end open
---------------------------------------------------------------------
---------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
--FUNCTION TO CHECK IF A FOLDER EXISTS YET
on CheckForFolder(thisFolder)
tell application "Finder"
return (exists folder thisFolder)
end tell
end CheckForFolder