Get creation date form PDF meta data

Hi,

I’ve copied a lot of PDF files to my computer and indexed them into Devonthink. The creation date Devonthink uses is from the file system and are all from the same day. The PDF files a quite old. In Preview I see the real creation dates in the info window. Can I use that data in any way in Devonthink?

If not, the filenames are named like “0315_Name”. Can I rename them with A Better Finder Rename to “1503_Name” or do I lose my Tags as Devonthink doesn’t know about the renaming? My Files are indexed and not inside the Database.

Thanks, Tekl

Hi I helped myself by using Exiftool and AppleScript. Only tested on a German system. I don’t know how the AppleScript’s date command behaves on different locales.

Edit: Added ExifTool check and progress bar

-- PDF Date to File Date
-- Created by Tekl on 2016-01-29

try
	tell application "Finder"
		if not (exists POSIX file "/usr/local/bin/exiftool") then
			tell application id "DNtp" to display dialog "This script needs ExifTool to operate." buttons {"Visit ExifTool website"} default button 1 with icon 2 with title "DEVONthink Pro"
			open location "http://www.sno.phy.queensu.ca/~phil/exiftool/"
			return
		end if
	end tell
	tell application id "DNtp"
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
		show progress indicator "Copying PDF date to file date ..." steps (count of this_selection) with cancel button
	end tell
	repeat with this_record in this_selection
		set this_date to do shell script "/usr/local/bin/exiftool -d '%d.%m.%Y %H:%M:%S' -S -CreateDate " & quoted form of (path of this_record as string)
		set this_date to my stringReplace(this_date, "CreateDate: ", "") as string
		set this_date to date (this_date)
		tell application id "DNtp"
			set the modification date of this_record to this_date
			set the creation date of this_record to this_date
			set the addition date of this_record to this_date
			step progress indicator (name of this_record as string)
		end tell
	end repeat
	tell application id "DNtp" to hide progress indicator
on error error_message number error_number
	tell application id "DNtp"
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
		hide progress indicator
	end tell
end try

on stringReplace(subjectStr, findStr, replaceStr)
	set orgDelims to text item delimiters of AppleScript
	set text item delimiters of AppleScript to findStr
	set resultStr to text items of subjectStr
	set text item delimiters of AppleScript to replaceStr
	set resultStr to the resultStr as string
	set text item delimiters of AppleScript to orgDelims
	return resultStr
end stringReplace

Renaming indexed files can be done outside DEVONthink but it can be done as you have here. Interesting approach too.

Do note that it is always a good idea to follow this naming convention for filenames:
Only use hyphens, underscores, and a single dot before the file extension in a filename.

I use a similar approach with Exiftool to change the metadata title of a PDF to whatever is in my clipboard. My workflow is that I have a three pane view open and a number of columns visible. First column is name of the PDF (Author, First Initial, Date). Second column is the metadata title of the PDF. Often this is a meaningless title so I highlight text in the PDF corresponding to the name of the article, copy it and run the below script. The only issue (that I have not solved yet) is changing the text from all caps to title case.

-- Get the contents of the clipboard and reformat as a single line of text if carriage returns are present

set titleClipboard to the clipboard as text
set titleList to paragraphs of titleClipboard
set titleName to ""
repeat with i in titleList
	set titleName to titleName & i & space
end repeat
set titleName to titleName as text

-- Get the path of the selection

tell application id "DNtp"
	try
		set theSelection to the selection
		if the selection is {} then error "Please select something"
		set pdfPath to path of the first item of theSelection
	end try
end tell


-- Set metadata title

do shell script "/usr/local/bin/exiftool -title=" & quoted form of titleName & " -overwrite_original " & quoted form of pdfPath

Here’s a method to change a string to Title Case or lower case or UPPER CASE – it’s a good routine to keep in your snippet library. Though there are hundreds of alternatives here and there in the internet.

(*
	sourced from 
	https://discussions.apple.com/thread/6779735?start=0&tstart=0
*)

return _case_text("pick blue berries and make tart jam", "t")

on _case_text(s, c)
	(*
        string s : source string
        integer or string c : case
            0 or l or lower = lower case
            1 or u or upper = UPPER CASE
            2 or t or title = Title Case
    *)
	set {l, u, t} to {{0, "l", "lower"}, {1, "u", "upper"}, {2, "t", "title"}}
	if c is in l then
		set pl to "s/.+/\\L$&/og"
	else if c is in u then
		set pl to "s/.+/\\U$&/og"
	else if c is in t then
		set pl to "s/\\b\\w+/\\u\\L$&/og"
	else
		error "invalid case option: " & c number 8000
	end if
	do shell script "printf '%s' " & s's quoted form & " | perl -CSDA -pe " & pl's quoted form without altering line endings
end _case_text

Thanks korm for that script, handy to have. I will put it in my snippets collection. For my purpose I just wanted title case and so decided to call upon a simple Python command - print str.title(), via AppleScript, which seems to work. My full script is below

-- Script to change the metadata title to clipboard contents
-- Note - requires ExifTool to be installed in /usr/local/bin

-- Get the contents of the clipboard and reformat as a single line of text if carriage returns are present

set titleClipboard to the clipboard as text
set titleList to paragraphs of titleClipboard
set titleName to ""
repeat with i in titleList
	set titleName to titleName & i & space
end repeat
set titleName to titleName as text

-- Format titleName as title case

set titleCaseName to do shell script "python -c 'str = \"" & titleName & "\"; print str.title()'"

-- Get the path of the selection

tell application id "DNtp"
	try
		set theSelection to the selection
		if the selection is {} then error "Please select something"
		set pdfPath to path of the first item of theSelection
	end try
end tell


-- Set metadata title

do shell script "/usr/local/bin/exiftool -title=" & quoted form of titleCaseName & " -overwrite_original " & quoted form of pdfPath

@kseggleton: Noice! I’m digging the python string manipulation there. :smiley: