【问题标题】:Renaming PDF File with Apple Script使用 Apple 脚本重命名 PDF 文件
【发布时间】:2021-11-12 16:19:28
【问题描述】:

当使用 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

【问题讨论】:

  • 您应该将您的 pdf 文件的完整 HFS 路径分配给 File 变量。
  • 在您的问题中,您提到了 AutomatorAutomator 与此有什么关系?当您使用 Automator 将其添加到我的文件夹时说“Invoice Template.pdf”时,这是您在 Automator 中创建的 Folder Action 场景吗?
  • @user3439894 是的。很抱歉我忘了提及这一点。

标签: applescript


【解决方案1】:
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

【讨论】:

  • 有什么方法可以获取我的特定 PDF 文件的完整 HFS 路径?它总是在同一个地方。但是当我尝试路径“/Users/MYUSERNAME/Dropbox/Invoices/Invoice Template.pdf”时它不起作用。
  • "/Users/MYUSERNAME/Dropbox/Invoices/Invoice Template.pdf" 不是 HFS 路径,而是 POSIX 路径。如果您可以在 Finder 中查看并选择您的 pdf,您还可以获得其正确的 HFS 路径运行脚本将 FileHFS 设置为(选择“com.adobe.pdf”类型的文件)作为文本并选择你的pdf
  • Finder 无法识别 POSIX 路径,只能识别以冒号分隔的 HFS 路径(已过时且卷名存在问题)。您可以使用系统事件,它确实理解 POSIX 路径,或者您可以将 POSIX 路径字符串强制转换为 POSIX 文件值并将其提供给 Finder。
  • @RobertKniazidis 对此感到抱歉。感谢您的帮助!
【解决方案2】:

你需要告诉 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

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2019-06-23
    • 2010-09-20
    • 2021-11-30
    相关资源
    最近更新 更多