Search code examples
applescript

Renaming PDF File with Apple Script


I'm trying to rename a finder item called "Invoice Template.pdf" when it is added to my folder with Automator. However, every time the script runs, I get Finder got an error: Can’t set file "Invoice Template.pdf" to "Invoice 11.08.2021.pdf". Any ideas why?

tell application "Finder"
    set dateObj to (current date)
    set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
    set theDay to text -1 thru -2 of ("0" & day of dateObj)
    set theYear to year of dateObj
    set dateStamp to "" & theMonth & "." & theDay & "." & theYear
    set theFile to "DBF Invoice Template.pdf"
    set theName to "Invoice"
    set the name of file theFile to theName & " " & dateStamp & ".pdf"
    
end tell

Solution

  • set dateObj to (current date)
    set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
    set theDay to text -1 thru -2 of ("0" & day of dateObj)
    set theYear to year of dateObj
    set dateStamp to "" & theMonth & "." & theDay & "." & theYear
    
    set theFileHFS to (choose file of type "com.adobe.pdf") as text
    set theName to "Invoice"
    
    tell application "Finder"
        set the name of file theFileHFS to theName & " " & dateStamp & ".pdf"
    end tell