The workflow for this script is:
Input PDF file with PDF MetaData ‘Title’, set the Title to variable and write a new file with Title as file name in same directory as original file.
TODO: Ask for a option to type a Title (if Title is missing).
EDIT:
Include a option to send the original PDF to trash. (a new file need to be created to this to work)
use framework "Foundation"
use framework "Quartz"
use scripting additions
(**
* [pdfDocumentByTagTitle]
*)
set thePath to POSIX path of (choose file)
my pdfDocumentByTagTitle:thePath trashItem:"false"
on pdfDocumentByTagTitle:thePath trashItem:_boolean
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set theDir to POSIX path of ((theURL's URLByDeletingLastPathComponent()) as text)
set theDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set filemanager to current application's NSFileManager's defaultManager()
if (theDoc is not missing value) then
set theDict to (current application's NSMutableDictionary's alloc()'s initWithDictionary:(theDoc's documentAttributes())) as record
try
if ((Title of theDict) is not missing value) then
set theTitle to Title of theDict
set _writeToURL to current application's |NSURL|'s fileURLWithPath:(theDir & theTitle & ".pdf")
theDoc's writeToURL:_writeToURL
if (_boolean = "true") then
set {theResult, destURL, theError} to filemanager's trashItemAtURL:theURL resultingItemURL:(reference) |error|:(reference)
end if
end if
end try
end if
end pdfDocumentByTagTitle:trashItem: