AppleScript 将日期附加到文件

AppleScript to append date to file

我最近能够在 Automator 中制作一个拖放脚本,允许我压缩和命名文件,然后自动应用日期 (DDMMYY),但现在它默认为 (DDMMYYYY),我无法更改它。我在谷歌上搜索了一个解决方案,但没有任何效果,因为这需要放在文件名的末尾。

有没有人知道我做错了什么或者有没有人有可以帮助我的实际脚本?我发现的所有内容只有在日期位于文件名的开头而不是结尾(但在扩展名之前)时才有效。

由于您没有提供任何代码,我无法猜测您是如何命名文件的,但是获取 DDMMYY 的方法是使用 shell 和 do shell script 命令:

tell application "Finder"
    set theFile to (choose file)
    set theName to name of theFile
    set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell

这并没有去掉原文件的文件扩展名。为此,您必须使用这样的东西:

tell application "Finder"
    set theFile to (choose file)
    set theName to name of theFile
    set periodIndex to offset of "." in theName
    set theName to text 1 thru (periodIndex - 1) of theName
    set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell

负责去除文件扩展名的代码来自.

您可以根据需要添加标点符号,只需将所需的符号放在下一个 % 之前即可。所以 date +%d.%m.%y 是可能的并且提高了可读性。

您可以在 运行 AppleScript 操作中使用此 AppleScript,它会在文件扩展名

之前插入日期重命名存档
on run {input, parameters}
    set thePath to POSIX path of (item 1 of input)
    set currentDate to do shell script "date +%d%m%y"
    set newPath to text 1 thru -5 of thePath & "_" & currentDate & ".zip"
    do shell script "mv " & quoted form of thePath & space & quoted form of newPath
    return {POSIX file newPath as alias}
end run