This is a follow up to my post about getting a PDF’s metadata for creation and modification date. Thanks to @BLUEFROG and @chrillek for the initial help.
I’m not a JavaScript guy, so I went with AppleScript and a CLI tool called exiftool
. It can be installed via brew with brew install exiftool
. This script is run from the Script Menu.
I am 95% of the way there, but it changes all the dates to the “real” date at 12:00 AM because I can’t figure out how to format the output of exifdata to a string that can be converted and understood correctly.
What is the right string format to send to the system AppleScript to convert from string to date? Note I broke out the make_date command because within the repeat it was not converting correctly.
Any tips?
-- Hacked together by Josh Liebster, 2022
try
tell application id "DNtp"
set thisSelection to the selection
if thisSelection is {} then error "Please select some contents."
repeat with thisRecord in (selection as list)
if (type of thisRecord is PDF document) then
set thisFile to path of thisRecord
set pdfCDateString to (do shell script "/opt/homebrew/bin/exiftool -s3 -CreateDate -d %D " & (quoted form of thisFile)) as string
set pdfCDate to my make_date(pdfCDateString)
set (creation date of thisRecord) to pdfCDate
set pdfMDateString to (do shell script "/opt/homebrew/bin/exiftool -s3 -ModifyDate -d %D " & (quoted form of thisFile)) as string
set pdfMDate to my make_date(pdfMDateString)
set (modification date of thisRecord) to pdfMDate
end if
end repeat
end tell
on error error_message number error_number
if the error_number is not -128 then
try
display alert "DEVONthink" message error_message as warning
on error number error_number
if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
end try
end if
end try
on make_date(date_string)
set converted_date to date (date_string)
return converted_date
end make_date