使用 AppleScript 随机重命名文件不起作用
Renaming files randomly with AppleScript isn't working
我想获取目录的文件并通过在相同文件中随机重新分配现有文件名来重命名它们。
例如,如果一个目录有以下三个文件(名称和文件大小):
filenameA 100KB
filenameB 200KB
filenameC 300KB
在 运行 脚本之后,它可能看起来像这样:
filenameB 100KB
filenameC 200KB
filenameA 300KB
所以三个文件有 6 个 permutations,四个文件有 24 个,等等……
tell application "Finder"
tell application "System Events" to set theFiles to every file of folder "/path/to/my/directory"
repeat count of theFiles times
tell application "System Events" to set theFiles to every file of folder "/path/to/my/directory"
set randint1 to random number from 1 to count of theFiles
set randint2 to random number from 1 to count of theFiles
set theName1 to name of item randint1 of theFiles
set theName2 to name of item randint2 of theFiles
set name of item randint1 of theFiles to "randomname"
set name of item randint2 of theFiles to theName1
set name of item randint1 of theFiles to theName2
end repeat
end tell
运行 此代码没有 return 任何错误,但它也不起作用。
我希望你清楚脚本应该做什么。
它不是很漂亮,但下面的 AppleScript 代码应该可以完成,我相信您希望实现的目标。
activate
set containingFolder to (choose folder) as text
tell application "Finder"
set theFiles to files of alias containingFolder as alias list
end tell
set theNumber to 0
set numberList to {}
set randomNumbersList to {}
repeat (count of theFiles) times
set theNumber to theNumber + 1
set end of numberList to theNumber
end repeat
repeat with thisNumber in numberList
set thisNumber to some item of numberList
if thisNumber is not in randomNumbersList then
set end of randomNumbersList to thisNumber
else
repeat while randomNumbersList contains thisNumber
set thisNumber to some item of numberList
delay 0.01
end repeat
set end of randomNumbersList to thisNumber
end if
end repeat
tell application "System Events"
repeat with i from 1 to count of theFiles
set thisItem to item i of theFiles
set name of thisItem to "randomname " & ¬
(item i of randomNumbersList as text) & "." & name extension of thisItem
end repeat
end tell
抓取初始文件和文件名后,此脚本将文件名随机化到一个单独的列表中。然后它会临时重命名每个文件以防止名称冲突。最后,它循环遍历每个文件并将其文件名更改为随机文件名,然后从符合条件的名称池中删除该名称。
tell application "Finder"
-- choose folder, list its files and file names
set baseFol to choose folder
set origFiles to files of baseFol as alias list -- list of files
set origNames to name of files of baseFol as alias list -- list of file names
-- create list of randomized names
set randNames to {}
set cc to 0
repeat count of origNames times -- pool of file names
set cc to cc + 1 -- increment counter to one above current list length
repeat until (count of randNames) is cc
set somName to some item of origNames
if randNames does not contain somName then
set end of randNames to somName -- increment list length to match counter
end if
end repeat
end repeat
-- temporarily rename files to avoid name collisions
repeat with tmpName in origFiles
set name of tmpName to "g" & space & name of tmpName
end repeat
set tmpFiles to files of baseFol as alias list -- all g-files list
-- rename each file to final name
repeat with finNames in tmpFiles
set name of finNames to (item 1 of randNames) -- use random name
set randNames to rest of randNames -- remove used name from name pool
end repeat
end tell
我想获取目录的文件并通过在相同文件中随机重新分配现有文件名来重命名它们。
例如,如果一个目录有以下三个文件(名称和文件大小):
filenameA 100KB
filenameB 200KB
filenameC 300KB
在 运行 脚本之后,它可能看起来像这样:
filenameB 100KB
filenameC 200KB
filenameA 300KB
所以三个文件有 6 个 permutations,四个文件有 24 个,等等……
tell application "Finder"
tell application "System Events" to set theFiles to every file of folder "/path/to/my/directory"
repeat count of theFiles times
tell application "System Events" to set theFiles to every file of folder "/path/to/my/directory"
set randint1 to random number from 1 to count of theFiles
set randint2 to random number from 1 to count of theFiles
set theName1 to name of item randint1 of theFiles
set theName2 to name of item randint2 of theFiles
set name of item randint1 of theFiles to "randomname"
set name of item randint2 of theFiles to theName1
set name of item randint1 of theFiles to theName2
end repeat
end tell
运行 此代码没有 return 任何错误,但它也不起作用。
我希望你清楚脚本应该做什么。
它不是很漂亮,但下面的 AppleScript 代码应该可以完成,我相信您希望实现的目标。
activate
set containingFolder to (choose folder) as text
tell application "Finder"
set theFiles to files of alias containingFolder as alias list
end tell
set theNumber to 0
set numberList to {}
set randomNumbersList to {}
repeat (count of theFiles) times
set theNumber to theNumber + 1
set end of numberList to theNumber
end repeat
repeat with thisNumber in numberList
set thisNumber to some item of numberList
if thisNumber is not in randomNumbersList then
set end of randomNumbersList to thisNumber
else
repeat while randomNumbersList contains thisNumber
set thisNumber to some item of numberList
delay 0.01
end repeat
set end of randomNumbersList to thisNumber
end if
end repeat
tell application "System Events"
repeat with i from 1 to count of theFiles
set thisItem to item i of theFiles
set name of thisItem to "randomname " & ¬
(item i of randomNumbersList as text) & "." & name extension of thisItem
end repeat
end tell
抓取初始文件和文件名后,此脚本将文件名随机化到一个单独的列表中。然后它会临时重命名每个文件以防止名称冲突。最后,它循环遍历每个文件并将其文件名更改为随机文件名,然后从符合条件的名称池中删除该名称。
tell application "Finder"
-- choose folder, list its files and file names
set baseFol to choose folder
set origFiles to files of baseFol as alias list -- list of files
set origNames to name of files of baseFol as alias list -- list of file names
-- create list of randomized names
set randNames to {}
set cc to 0
repeat count of origNames times -- pool of file names
set cc to cc + 1 -- increment counter to one above current list length
repeat until (count of randNames) is cc
set somName to some item of origNames
if randNames does not contain somName then
set end of randNames to somName -- increment list length to match counter
end if
end repeat
end repeat
-- temporarily rename files to avoid name collisions
repeat with tmpName in origFiles
set name of tmpName to "g" & space & name of tmpName
end repeat
set tmpFiles to files of baseFol as alias list -- all g-files list
-- rename each file to final name
repeat with finNames in tmpFiles
set name of finNames to (item 1 of randNames) -- use random name
set randNames to rest of randNames -- remove used name from name pool
end repeat
end tell