PDFTitle -> PDFName - Rename PDF files by their title

I download a lot of PDF documents to my Mac. And as I was raised as a tidy person, I rename most PDF files to store them with a meaningful file name. Most of the downloaded PDF files have rather ugly and cryptic file names like «ae7_scripting_guide.pdf». But I often use the PDF title as the file name, like «After Effects Scripting Guide».

Now, if you need to rename a couple of PDF files, then opening the document in Preview, copying the PDF title from the info panel and finally renaming the file in the Finder can become a tedious task. That’s why today I invested some time to write an AppleScript to automate this workflow:

PDFTitle → PDFName - Rename PDF documents by their title (ca. 33.5 KB)

The script requires Mac OS X 10.5 and was successfully tested on Intel & PowerPC based Macs.

It uses the «mdls» command to retrieve the title of the PDF document (kMDItemTitle) and renames the file with the «mv» command, but it won’t overwrite an existing file. If a PDF file does not contain a value for its title property, then it is skipped.

PDF files need to have the ‘.pdf’ suffix to be recognized by the AppleScript droplet.

Happy Scripting from Germany!


-- author: Martin Michel
-- created: 22.11.2008
-- version: 1.0
-- requires:
-- ¢ Mac OS X 10.5 (because of Spotlight)
-- tested on:
-- ¢ Mac OS X 10.5.5
-- ¢ Intel & PowerPC based Macs

property mytitle : "PDFTitle -> PDFName"

-- This handler is called when the user opens the script with a double click
on run
	tell me
		activate
		display dialog "This is an AppleScript droplet." & return & return & "Drag and drop PDF files onto the script icon to process them." buttons {"OK"} default button 1 with title mytitle with icon note giving up after 30
	end tell
end run

-- This handler is called when the user drops Finder items onto the script icon
on open finderitems
	try
		-- searching for dropped PDF files (file name must end with '.pdf')
		set pdffiles to {}
		repeat with finderitem in finderitems
			set iteminfo to info for finderitem
			if not (folder of iteminfo) and (name of iteminfo ends with ".pdf") then
				set pdffiles to pdffiles & finderitem
			end if
		end repeat
		-- no PDF files found :(
		if pdffiles is {} then
			set errmsg to "You did not drop any PDF files onto the script." & return & return & "Please note that PDF files need to have the '.pdf' suffix to be recognized by the script."
			my dsperrmsg(errmsg, "--")
			return
		end if
		-- processing found PDF files
		repeat with pdffile in pdffiles
			set oldpdfname to (name of (info for pdffile))
			-- command for retrieving the PDF title on the command line
			set command to "mdls -name kMDItemTitle " & quoted form of POSIX path of pdffile
			set output to (do shell script command)
			-- skipping PDF files which do not contain a PDF title
			if output is not equal to "kMDItemTitle = (null)" then
				-- extracting the PDF title
				set pdftitle to (characters 17 through -2 of output) as text
				-- asking the user to confirm the PDF title to be used as the new file name
				set newpdfname to my getnewpdfname(oldpdfname, pdftitle)
				-- did the user hit the cancel button?
				if newpdfname is not missing value then
					if newpdfname does not end with ".pdf" then
						set newpdfname to newpdfname & ".pdf"
					end if
					set parentfolderpath to my getparentfolderpath(pdffile as text)
					-- renaming the PDf file with the «mv» command
					set command to "mv -n " & quoted form of POSIX path of pdffile & space & quoted form of POSIX path of ((parentfolderpath & newpdfname) as text)
					try
						do shell script command
					on error errmsg number errnum
						my dsperrmsg(errmsg, errnum)
					end try
				end if
			end if
		end repeat
	on error errmsg number errnum
		-- ignoring 'User canceled'-error
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end open

-- I am asking the user to confirm the PDF title used as the new file name
on getnewpdfname(oldpdfname, pdftitle)
	try
		tell me
			activate
			display dialog "Please choose a new name for the PDF file: " & return & return & oldpdfname default answer pdftitle buttons {"Cancel", "Rename"} default button 2 with title mytitle
			set dlgresult to result
		end tell
		set usrinput to text returned of dlgresult
		if usrinput is "" then
			my getnewpdfname(oldpdfname, pdftitle)
		else if (usrinput contains ":") or (usrinput contains "/") then
			set errmsg to "File names must not contain a colon ':' or slash '/' character."
			my dsperrmsg(errmsg, "--")
			my getnewpdfname(oldpdfname, pdftitle)
		else
			return usrinput
		end if
	on error errmsg number errnum
		return missing value
	end try
end getnewpdfname

-- I am returning the parent folder path of a given item path
on getparentfolderpath(itempath)
	set olddelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set itemcount to (count text items of itempath)
	set lastitem to the last text item of itempath
	if lastitem = "" then
		set itemcount to itemcount - 2 -- folder path
	else
		set itemcount to itemcount - 1 -- file path
	end if
	set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
	set AppleScript's text item delimiters to olddelims
	return parentfolderpath
end getparentfolderpath

-- I am displaying error messages to the user (hopefully only few)
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle giving up after 30
	end tell
end dsperrmsg

Hi Martin,

nice script :slight_smile:

two little notes.
¢ As Leopard is required, Unicode text is obsolete. Leopard has an unique text class text.
¢ the class alias in AppleScript has a POSIX path property, so the as text coercion is not needed, for example

quoted form of POSIX path of pdffile

is sufficient

Hi Stefan,

It’s excellent to have you around! I really need to modify my scripting habits :smiley: Of course, I already adjusted the code according to your suggestions.

P.S.: I am already thinking about writing a foundation tool to extract the PDF title, so that the script can also work on Mac OS X versions prior to 10.5.

Hi everybody,

Due to a request from an Italian user I updated the script in the meantime. Now it can also write the PDF filename into the title metadata information:

Natina-Download

Maybe this is also of use for some other people :slight_smile:

Best regards from Berlin,

Martin