This script writes tags into PDF keywords.
Depending on property overwriteKeywordsWithTags
it either adds new tags or overwrites existing keywords with your current tags.
To use it you need to download ExifTool by Phil Harvey
-- Write tags into PDF keywords
-- This script needs exiftool, see https://exiftool.org/
property overwriteKeywordsWithTags : false -- set to true if you want to mirror DEVONthink tags
tell application id "DNtp"
try
set theRecords to selected records
if theRecords = {} then error "Please select some PDF records"
show progress indicator "Writing tags into keywords ... " steps (count theRecords) as string with cancel button
repeat with thisRecord in theRecords
step progress indicator "... " & name of thisRecord
set theType to type of thisRecord as string
if theType = "PDF document" or theType = "«constant ****pdf »" then
set thePath to path of thisRecord
set theTags to tags of thisRecord
try
set theMetaData to meta data of thisRecord
set theKeywords to my tid(KMDItemKeywords of theMetaData, "," & linefeed)
on error
set theKeywords to {}
end try
if overwriteKeywordsWithTags = false then
set theNewTags to {}
repeat with thisTag in theTags
if theKeywords does not contain thisTag then set end of theNewTags to thisTag as string
end repeat
if theNewTags ≠ {} then
set theNewKeywords_sorted to my sort_list(theKeywords & theNewTags)
set theNewKeywords_sorted_string to my tid(theNewKeywords_sorted, ", ")
do shell script "/usr/local/bin/exiftool -PDF:Keywords= -overwrite_original_in_place " & quoted form of thePath
do shell script "/usr/local/bin/exiftool -sep ', ' -PDF:Keywords+=\"" & theNewKeywords_sorted_string & "\" -overwrite_original_in_place " & quoted form of thePath
end if
else if overwriteKeywordsWithTags = true then
set theTags_sorted to my sort_list(theTags)
if theTags_sorted ≠ my sort_list(theKeywords) then
set theTags_sorted_string to my tid(theTags_sorted, ", ")
do shell script "/usr/local/bin/exiftool -PDF:Keywords= -overwrite_original_in_place " & quoted form of thePath
if theTags_sorted_string ≠ "" then
do shell script "/usr/local/bin/exiftool -sep ', ' -PDF:Keywords+=\"" & theTags_sorted_string & "\" -overwrite_original_in_place " & quoted form of thePath
end if
end if
end if
end if
end repeat
hide progress indicator
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
on tid(theInput, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
if class of theInput = text then
set theOutput to text items of theInput
else if class of theInput = list then
set theOutput to theInput as text
end if
set AppleScript's text item delimiters to d
return theOutput
end tid
on sort_list(theList)
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10}
set list_string to (theList as string)
set new_string to do shell script "echo " & quoted form of list_string & " | sort -f"
set new_list to (paragraphs of new_string)
set AppleScript's text item delimiters to old_delims
return new_list
end sort_list