使用 Apple Script 重命名 PDF 文件

Renaming PDF File with Apple Script

当使用 Automator 添加到我的文件夹时,我正在尝试重命名名为“Invoice Template.pdf”的查找器项目。但是,每次脚本运行时,Finder 都会出现错误:无法将文件“Invoice Template.pdf”设置为“Invoice 11.08.2021.pdf”。有什么想法吗?

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
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

您需要告诉 Finder 文件在哪里:

property myFolder: "/Users/MYUSERNAME/Dropbox/Invoices" as POSIX file

tell application "Finder"
    ...
    set theFile to "DBF Invoice Template.pdf"
    set theName to "Invoice"
    set the name of file theFile of folder myFolder to theName & " " & dateStamp & ".pdf"
    
end tell