Folder automatic action to rename files added to folder with random names

applescriptautomatorfinder

I'm creating a database on Filmaker Pro (FMP) of images that will be stored in a folder. I would like this folder to have an automatic action where I can, occasionally, drag one or more image files into the folder and their "basenames" will each be automatically renamed with unique random (ten) alphanumeric series string that will not clash with whatever filenames already in the folder.

Is there a clever AppleScript or other technique to get this done? I'm a mid-level user, I only know how to use Automator, where it can't be done.

Failing an automatic solution, I could use a random string generator and change filenames manually.

Best Answer

Okay total different approach.

Use AppleScript only.

Step 1: Create folders (input and output)

Step 2: Create script

on adding folder items to theAttachedFolder after receiving theNewItems

    set text item delimiters to "."

    set outputFolder to POSIX file "/Users/YOURUSERNAME/Desktop/random/output/"

    tell application "Finder"
        set all_files to every item of theNewItems as list

        --now we start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
        repeat with index from 1 to the count of all_files

            set x to ""
            repeat 10 times
                set x to x & some item of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
            end repeat

            set new_name to x

            --using our index, we select the appropriate file from our list
            set this_file to item index of all_files
            set file_name_count to text items of (get name of this_file)

            --lets check if the current file from our list (based on index-number) has even any file-extension
            if number of file_name_count is 1 then
                --file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.
                set file_extension to ""
            else
                --yup, we are currently processing a file that has a file-extension
                --we have to re-add the original file-extension after changing the name of the file!
                set file_extension to "." & item -1 of file_name_count
            end if
            --let's rename our file and add the file-extension to it
            set dupeFile to duplicate this_file to outputFolder
            delete this_file
            set the name of dupeFile to new_name & file_extension as string


        end repeat
    end tell

end adding folder items to

Step 3: Save script in ~/Library/Scripts/Folder Action Scripts/

Step 4: Right mouse click on input folder > Services > Folder Action Setup

Folder Action Setup

Step 5: Connect script to folder.

Left part you add your input folder, click on folder in the left list. At the right list you choose your script.

Connect script to folder

Step 6: Add a bunch of files in the input folder. They are getting random names in the output folder and are deleted from the input folder.

Do not forget to change the outputFolder value inside the script!