【发布时间】:2021-07-22 16:42:04
【问题描述】:
我想获取目录的文件并通过在相同文件中随机重新分配现有文件名来重命名它们。
例如,如果一个目录有以下三个文件(名称和文件大小):
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
运行此代码不会返回任何错误,但它也不起作用。
我希望很清楚脚本应该做什么。
【问题讨论】:
-
Finder和System Events返回不同类型的项目。仅使用 Finder(指定冒号分隔的 HFS 路径)或仅使用系统事件。两者都可以重命名文件。 -
@vadian 用系统事件替换Finder后,还是不行
-
这太随意了(而且不受控制)。当“randomname”将被分配给具有相同名称的项目时,您最迟应该会得到一个错误。
标签: applescript finder