Applescript - Rename PDF to Title from Metadata

Posted by Dave on 22 October 2012

Today I downloaded a the full set of Configuration and Command references for the HP 12500 Series Switch from HP.com. When I looked in my download finder they were all helpfully named "cXXXXXX.pdf". Interestingly enough the title in the metadata seemed to be correct, so I wrote an AppleScript to batch rename them.

set theFiles to choose file with multiple selections allowed
repeat with theFile in theFiles
    set filePath to quoted form of POSIX path of theFile
    set theName to do shell script "mdls -name kMDItemTitle " & filePath & " -raw"
    set theName to theName & ".pdf"
    if theName is not "(null)" then try
        tell application "System Events" to set name of theFile to theName
    end try
end repeat

References: Apple Support Fourms