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.

  • Are the renamed files to include its original filename extension? In other words, is someFilename.jpg being renamed to randomFilename.jpg to just to randomFilename? – user3439894 May 31 '17 at 15:47

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 ""
                --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!

  • Thank you. I presume that script step must work together with an Automator step "Rename Finder Items". I can't see how to add the random string into the filename field. – Alex_V May 31 '17 at 8:32
  • @user3439894 I'm curious what the OP already have inside his Automator Workflow. Link should work. When I'm back home I will take a look if I get it working for multiple files. The problem I had with using 1 folder that it creates an endless loop. If you have any suggestion, please feel free to share ;-) – LH16 May 31 '17 at 20:41
  • Okay, I switched to AppleScript only. Learned you can also make folder actions with AppleScript :D – LH16 May 31 '17 at 21:39
  • Thank you very much. I hope I’m not causing too much trouble. I merely requested an “imagined solution” that would suit my needs, without considering how it would be achieved. I will try the solutions provided here and see how it goes. – Alex_V Jun 1 '17 at 5:04
  • Your methods works! :D Much appreciated. Great fun to see it happen as requested. Although, I'm hoping that @user3439894 can help me with his singular folder solution. So far so good. (Y) – Alex_V Jun 1 '17 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .