如何使用脚本编辑器将随机字符串附加到文件名 [Mac OS]

How to append a random string to filenames using Script Editor [Mac OS]

我有一个装满 pdf 文件的文件夹。

filename_1.pdf
filename_2.pdf
filename_3.pdf
等...

我正在寻找一种方法将这些文件名转换为类似 :

filename_1973878763487.pdf
filename_27523765376346.pdf
filename_326537652376523.pdf

我遇到了以下将文件名更改为随机数的脚本:

  tell application "Finder"
    repeat with this_item in (get items of window 1)
        set name of this_item to ((random number from 1000 to 9999) & "." & name extension of this_item) as string
    end repeat
end tell

脚本输出如下:

3598.pdf
7862.pdf
8365.pdf

所以我需要一种方法将随机数附加到原始文件名。

这应该适合你

tell application "Finder"
    repeat with thisItem in (get items of window 1)
        set fileName to name of thisItem
        tell current application
            set theOffset to offset of "_" in fileName
        end tell
        set tempFileName to text 1 thru (theOffset + 1) of fileName
        tell current application
            set randomNumber to (random number from 1000 to 9999)
        end tell
        set name of thisItem to tempFileName & (randomNumber & "." & name extension of thisItem) as string
    end repeat
end tell