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

时间:2018-10-21 23:59:41

标签: macos automator

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

文件名_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

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

1 个答案:

答案 0 :(得分:0)

这应该为您工作

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