使用Apple Script在Automator中重命名PDF出现Finder错误如何解决
错误原因
- 最核心的问题是脚本中没有指定目标文件的完整绝对路径:你只写了文件名的字符串,AppleScript在Automator文件夹动作的运行环境下,无法定位到你监控的指定文件夹里的对应文件,会默认去Finder当前激活的窗口路径查找,找不到对应文件就会抛出无法设置属性的错误。
- 第二个常见问题是文件名不匹配:你代码里硬编码的文件名是
DBF Invoice Template.pdf,但你实际要监控的目标文件名是Invoice Template.pdf,二者不一致也会导致找不到文件。 - 第三个可能原因:目标文件夹内已经存在和你要重命名的结果同名的文件,Finder默认不允许直接覆盖,也会触发该报错。
修正方案
你用的是Automator文件夹动作,不需要硬编码文件名和路径,直接接收Automator传入的新增文件参数即可,修正后的代码如下:
on run {input, parameters} tell application "Finder" -- 遍历触发动作的新增文件 repeat with eachFile in input -- 只处理匹配文件名的PDF if name of eachFile is "Invoice Template.pdf" then 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 newName to "Invoice " & dateStamp & ".pdf" -- 重命名前先检查是否有重名文件,避免冲突 if not (exists file newName of container of eachFile) then set name of eachFile to newName end if end if end repeat end tell return input end run
额外注意事项
- 如果一定要硬编码路径的话,需要把目标文件夹的完整路径拼接到文件名前,比如
set theFile to POSIX file "/Users/你的用户名/指定文件夹/Invoice Template.pdf" as alias - 原有代码中月份和日期的补零逻辑本身是可用的,不需要额外调整格式
内容的提问来源于stack exchange,提问作者Frank Doe
相关产品推荐
相关产品推荐

