Search code examples
applescriptfinder

Renaming files randomly with AppleScript isn't working


I would like to take a directory's files and rename them by reassigning their existing filenames randomly among the same files.

For example, if a directory had the following three files (name and file size):

filenameA   100KB
filenameB   200KB
filenameC   300KB

After running the script, it might look like this:

filenameB   100KB
filenameC   200KB
filenameA   300KB

So there would be 6 permutations for three files, 24 for four files, etc….

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

Running this code doesn't return any errors, however it doesn't work either.

I hope it's clear what the script is supposed to do.


Solution

  • After grabbing the initial files and file names, this script randomizes the file names into a separate list. It then temporarily renames each file to prevent name collisions. Finally, it cycles through each file and changes its file name to a random one and removes that name from the pool of eligible names.

    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