Automator: Service to move files into new subfolders based on file extension *within any selected folder*

applescriptautomationautomatorfinderservices

I've been searching for quite a while now but can't seem to find relevant instruction for this particular task. I'm brand new at this so apologies if I'm missing something basic.

Like many photographers I come back from a shoot with a card containing RAW (CR2), JPG, and MOV files all in a single folder. Once I copy this folder to my hard drive, I have to do the following actions, for which I would like to create a single service, so that I can run this same process for any folder I choose.

So here's what I'd like to automate:

  1. Create separate folders for Raw, JPG, and Video within selected folder.
  2. Identify files of each type and move them into the folders created in the first step.

I thought this would be simple, but the problem seems to lie in my need for this to be a generic service, not tied to any specific folder path. I need it to be repeatable for other folders, but I can't seem to figure out that essential versatility aspect.

I've already found and adapted an Apple Script (see below) which creates the subfolders I need. That part works fine as a service on its own. But I can't seem to perform the second step which would do the actual file-moving.

I know how to move files of a certain type to a specific folder but that's not what I need. I need all the JPGs in a certain folder to be moved to a new subfolder called JPG within the selected folder and likewise for the other file types. I understand how to filter those file types, but so far I can't figure out how to move them to a folder without having to specify a path, I don't know how to point to folders that don't yet exist. Even if I could, those paths will be different for each folder I need to work on.

Can any one give advice or point me to a relevant discussion?

Many, many thanks.

///

Here's the Apple Script for my subfolder creation service:
subfolder script

Script text:

on run {input, parameters}
    set output to {}
    tell application "Finder"
        set {source_folder, source_name} to {it, name} of first item of input
        repeat with prefix in {"JPG - ", "RAW - ", "Edits - ", "Video - "}
            make new folder at source_folder with properties {name:contents of prefix & source_name}
            set end of output to result as alias
        end repeat

Note: The file-moving part of the automation won't involve my the "Edits – " folder. This is just a container for future files.

Best Answer

If I were wanting to create several folders within a selected folder and move the existing files within the selected folder, to the folders created within the selected folder, based on the file extension, then I'd use bash not AppleScript.

The following Service takes the selected folder(s) in Finder and does the following:

  • Creates the following folders within the selected folder where "$parent_folder_name" is the name of the selected folder the service is run on:
    • "Edits - $parent_folder_name"
    • "JPG - $parent_folder_name"
    • "RAW - $parent_folder_name"
    • "Video - $parent_folder_name"
  • Moves the existing files to the newly created folders base on the file extension.

for f in "$@"; do
    cd "$f"
    d="$(basename "$f")"
    mkdir -p "Edits - $d" "JPG - $d" "Raw - $d" "Video - $d"
    mv *.JPG "./JPG - $d"
    mv *.CR2 "./RAW - $d"
    mv *.MOV "./Video - $d"
done

enter image description here