Как добавить случайную строку в имена файлов с помощью Script Editor [Mac OS] - PullRequest
0 голосов
/ 22 октября 2018

У меня есть папка, заполненная файлами PDF.

filename_1.pdf
filename_2.pdf
filename_3.pdf
etc ...

Я ищуспособ перейти от этих имен файлов к чему-то вроде:

имя_файла_1973878763487.pdf
имя_файла_27523765376346.pdf
имя_файла_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 голосов
/ 22 октября 2018

Это должно работать для вас

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
...