Rename PDF from Metadata - Possible?

I have a standard naming convention that I use for all of the PDF’s that I have collected - “Date - Author - Title.pdf”

When the files are first imported into DT, they frequently have completely random names associated with them. I’d like to find a way to edit/check the metadata on the file and then have DT rename the PDF to the correct structure. Where Metadata is missing (no author entered, for example), I’d love to be prompted to enter the correct data and then have the rename happen.

Is this at all possible? I can do it in Bookends, Papers or other PDF management software, but would like to have this all in one place.

Thanks,
Brad

How are you importing PDFs? If you import from the file system the names should not change. I’ve never seen DEVONthink assign “completely random names” to imported PDFs or any other kind of document.

Its probably not possible to do it directly on import, but if you are happy selecting the files afterwards, then its possible to rename the files, if the metadata in the pdf is in a standard form. It is quite easy to extract the metadata and rename the file, or have the script ask for the missing information.

Its going to require a bit of applescript. I suggest that you post an example pdf here and perhaps someone will look at it and take on the challenge.

Frederiko

Here’s an example:


tell application id "DNtp"
	try
		set theSelection to the selection
		repeat with theRecord in theSelection
			if type of theRecord is PDF document then
				set theName to name of theRecord as string
				set theMetadata to meta data of theRecord
				
				try
					set theTitle to |kMDItemTitle| of theMetadata
				on error
					set theTitle to missing value
				end try
				set theTitle to my validateMetadata(theName, "Title:", theTitle)
				
				try
					set theAuthor to |kMDItemAuthors| of theMetadata
				on error
					set theAuthor to missing value
				end try
				set theAuthor to my validateMetadata(theName, "Author:", theAuthor)
				
				set theDate to modification date of theRecord
				set theDate to ((year of theDate) as string) & "-" & ((month of theDate) as integer as string) & "-" & day of theDate as string
				
				set name of theRecord to theDate & " - " & theAuthor & " - " & theTitle
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

on validateMetadata(theName, theInfo, theValue)
	if not (exists theValue) or theValue is missing value or theValue is "" then
		tell application id "DNtp"
			repeat
				set theValue to display name editor theName info theInfo
				if theValue is not "" then exit repeat
			end repeat
		end tell
	end if
	return theValue
end validateMetadata

Thanks Christian. I’ll try that script and see if I can make it work.

Just to be clear, DT is not creating the random names. I was saying that the original author named it something meaningful to them, but not always meaningful to me. DT doesn’t touch the file name on import.

Thanks again for the prompt help!

Brad

Thank you Christian for the script. That is a whole lot cleaner than the way I had been extracting pdf metadata. Very useful.

Frederiko

Thanks Christian for this useful script! It does work as expected when invoked from within the Script editor, but it does not when run as stand alone app (in fact, nothing visible happens at all). What do I do wrong?

Cheers,

Nils

Do you mean saved in DEVONthink’s Scripts and runb with a PDF selected. I just tested it here and it works as expected.

Yes. I have tested it on another machine and it is the same: runs from Script Editor, but not from DT Script Menu.
Meanwhile, I think I have found the problem. I have copied Christian’s script anew and got the following error code (with a pdf with no meta data):

“missing value versteht die Nachricht „exists“ nicht.”

The culprit is the code snippet ```

not (exists theValue)


Cheers,

Nils

That handler checks if there’s an author and allows you to specify one if there isn’t. I just checked it out and it’s running from the DEVONthink Script menu as expected.