【问题标题】:Renaming files randomly with AppleScript isn't working使用 AppleScript 随机重命名文件不起作用
【发布时间】: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

运行此代码不会返回任何错误,但它也不起作用。

我希望很清楚脚本应该做什么。

【问题讨论】:

  • FinderSystem Events 返回不同类型的项目。仅使用 Finder(指定冒号分隔的 HFS 路径)或仅使用系统事件。两者都可以重命名文件。
  • @vadian 用系统事件替换Finder后,还是不行
  • 这太随意了(而且不受控制)。当“randomname”将被分配给具有相同名称的项目时,您最迟应该会得到一个错误。

标签: applescript finder


【解决方案1】:

这不是很漂亮,但是下面的 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

【讨论】:

    【解决方案2】:

    在获取初始文件和文件名后,此脚本将文件名随机化到一个单独的列表中。然后它会临时重命名每个文件以防止名称冲突。最后,它循环遍历每个文件并将其文件名更改为随机文件名,并将该名称从符合条件的名称池中删除。

    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
    

    【讨论】:

    • 是的,这正是我想要的。您能否将一些变量重命名为可能有相同问题的用户不知道ccsirNomBaseFol 等可能意味着什么。
    • 很高兴它有帮助。前两个实际上没有任何意义,但“si”代表“某些项目”,而“cc”只是一个计数器。第三个是“随机名称基础文件夹”。我会看看我能不能想出一些有意义的东西。
    • AppleScript 的意图是类似于英语并且非常易于阅读,其中一个方面是变量的名称很容易传达它们是什么和代表什么,而不是就像这个答案中的例子一样神秘。与其他答案相比,此答案中的变量命名编码风格晦涩难懂,并且在整个代码中更难阅读和遵循!请不要在变量命名编码风格中如此神秘,因为它损害了语言的意图,并且可能使大多数询问/阅读的人很难理解它。
    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多