尝试通过 AppleScript 将字符串添加到文件名时发生循环

Loop happens when trying to add string to a filename via AppleScript

我正在尝试编写一个可以将当前时间添加到文件夹名称的 AppleScript。

on adding folder items to this_folder after receiving these_items
    repeat with this_item in these_items
        tell application "Finder"
            set t to (time string of (current date))
            set name of this_item to (t & " " & (name of this_item))
        end tell
    end repeat
end adding folder items to

我已将脚本添加为特定文件夹的文件夹操作,因此它会自动重命名放入其中的每个文件夹。但是当我将一个文件夹添加到带有脚本作为文件夹操作的文件夹时,时间会重复添加,直到文件夹的名称太长而无法向其中添加更多字符。我怎样才能使时间只被添加一次?

提前致谢!

问题是每次重命名文件夹时,都会触发文件夹操作再次启动。我的解决方案是在将项目添加到触发器文件夹时禁用文件夹操作,然后在文件夹重命名后... re-enable 文件夹操作。这样,已有 re-named 个文件夹不会 re-trigger 文件夹操作重命名已有 re-named 个文件夹。

以下 AppleScript 适合我。

on adding folder items to this_folder after receiving these_items
    set t to (time string of (current date))
    tell application "System Events"
        set folder actions enabled to false
        repeat with this_item in these_items
            set name of this_item to (t & " " & (name of this_item))
        end repeat
        set folder actions enabled to true
    end tell
end adding folder items to

注意:最好随时使用 System Events 而不是 Finder