A Script use exiftool to change PDF file Metadata In DEVONthink

Hi, there!
Recently, I met a problem that I don’t know why I can’t change pdf file metadata in DEVONthink. So I use some outside tools, such as Acrobat, to do some change, it worked, seems pdf file is fine, maybe it is DEVONthink’s problem, or other problem.

I write an Apple script to use exexiftool to directly change selected pdf metadata in DEVONthink. Now, I want to share this script for all.

-- Change metadata of selected PDF files in DEVONthink
tell application id "com.devon-technologies.think3"
    -- Get the currently selected items
    set selectedItems to selection
    if (count of selectedItems) = 0 then
        display dialog "No files selected." buttons {"OK"} default button "OK"
        return
    end if
    
    -- Provide a list of metadata fields for the user to choose from
    set metadataOptions to {"Author", "Title", "Subject", "Keywords", "Organization", "Copyright"}
    set metadataField to choose from list metadataOptions with prompt "Please select the metadata field to update:" default items "Author" with title "Select Metadata Field"
    if metadataField is false then
        display dialog "No metadata field selected." buttons {"OK"} default button "OK"
        return
    end if
    set metadataField to item 1 of metadataField -- Get the selected field name

    -- Prompt the user to input a new value for the selected metadata field
    set newValue to text returned of (display dialog "Enter the new value:" default answer "")
    if newValue is "" then
        display dialog "The new value cannot be empty!" buttons {"OK"} default button "OK"
        return
    end if
    
    -- Iterate through the selected items
    repeat with theItem in selectedItems
        set itemType to type of theItem
        
        -- Process only PDF files
        if itemType = PDF document then
            try
                -- Get the file path
                set filePath to path of theItem
                
                -- Use exiftool to modify PDF metadata
                set exiftoolPath to "/opt/homebrew/bin/exiftool" -- Ensure this is the correct path to exiftool
                do shell script exiftoolPath & " -" & metadataField & "=" & quoted form of newValue & " -overwrite_original " & quoted form of POSIX path of filePath
                
                -- Notify the user of the successful update
                display notification "Updated: " & name of theItem
            on error errMsg
                display dialog "Failed to modify: " & errMsg buttons {"OK"} default button "OK"
            end try
        else
            display notification "Skipped non-PDF file: " & name of theItem
        end if
    end repeat
    
    display dialog "All selected PDF files have been successfully updated!" buttons {"OK"} default button "OK"
end tell

Ps1:You need to install exiftool. Use Homebrew to install: brew install exiftool.
Ps2:You need to change exiftool path as your local path.

1 Like

I’d suggest providing a list of possible metadata fields for the user to choose from instead of hoping that they don’t main trying while in entering them.

In fact, this script is likely to provide for who can’t directly change pdf metadata in DEVONthink. I don’t know why this problem happened, we all know DT3 can change rtf and pdf metadataIt. It means that who use this tool must know what metadata they want to change, that is more clearly than a list.

I know. But I was referring to this:

set metadataField to text returned of (display dialog "Please Input Metadata Fields(Example:Author, Title, Subject):" default answer "")

Since there’s only a limited number of metadata fields in a PDF, it would be more appropriate imo to provide a list of those fields, instead of relying on the user to type in the correct one.
Something like

set metadataField to chose from list with title "Please select metadata Field" default items {"Autor", "Title", "Subject", "Producer"…}  multiple selections allowed false empty selection allowed false

Not tested, though. But expecting users to not make errors when typing something in is very optimistic.

And here’s a different implementation in JavaScript that doesn’t rely on exiftool. The first post in this thread does more or less the same thing in AppleScript, also without exiftool.

I like exiftool, but imo it’s a bit convoluted to install an external tool if the same goal can be achieved with a different approach to coding.

2 Likes

Well, you are right, I did this script depend on acrobat complex metadata fields, list is more directly and easy to use. Besides, I almost have zero experience in any CS subjects, please forgive me :sweat_smile:, I am now preferring to use low code or zero code to fix problem. The only one thing I know about JS or Object-C is their name… IF I know more about this language, I will use them…