3

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.

1
  • Will you be selecting the actual folder to be sorted in order to run the service, or will be opening the folder and then choosing to run the service from the Finder menu ?
    – CJK
    Commented Sep 27, 2018 at 9:21

2 Answers 2

3

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

1
  • I feel bad not using @CJK 's beautiful applescript but decided your bash will be easier for a noob like me to tweak later. It works perfectly, thank you so much! I know so many people who will be glad to have this shortcut.
    – David
    Commented Sep 28, 2018 at 4:22
3

I've worked off the assumption that you will be selecting the actual folders in order to pass them to your Automator service workflow via, for example, the contextual right-click services menu.

Given this, here's what your service workflow would look like:

Automator Workflow on macOS

Here's the script that goes in the Run AppleScript action:

on run input
    set [input] to the input

    repeat with directory in the input
        organiseFilesOfFolder_(POSIX path of the directory)
    end repeat

end run

to organiseFilesOfFolder:f
    local f

    script SourceFolder
        use sys : application "System Events"

        property location : a reference to folder f of sys
        property name : (a reference to name of my location)

        script JPGs
            property folder : missing value
            property name extension : "jpg"
            property list : {}
            property id : "JPG"
        end script

        script RAWfiles
            property folder : missing value
            property name extension : "cr2"
            property list : {}
            property id : "RAW"
        end script

        script Videos
            property folder : missing value
            property name extension : "mov"
            property list : {}
            property id : "Video"
        end script

        script Edits
            property folder : missing value
            property id : "Edits"
        end script


        to make new "folder" given type:fileType
            [fileType's id, " - ", my name's contents]

            make new folder at my location ¬
                with properties {name:¬
                contents of result as text}

            set the folder of the fileType to the result
        end make

        on listOf:fileItems
            set the list of fileItems to (every file ¬
                of my location where its name extension ¬
                is the name extension of the fileItems)
        end listOf:
    end script

    tell the SourceFolder
        repeat with fileType in its [JPGs, RAWfiles, Videos, Edits]
            make new "folder" given type class:fileType
            if the contents of the fileType = its Edits then ¬
                exit repeat

            if (its listOf:fileType) is not {} then ¬
                tell application "System Events" to move ¬
                    list of fileType to the ¬
                    folder of the fileType
        end repeat
    end tell
end organiseFilesOfFolder:
9
  • Obviously if the OP's requirements is AppleScript only, then that's fine however, IMO it's overkill for such a simple task that can be done in eight lines of code in bash using a Run Shell Script vs. Run AppleScript. Nice script though! Commented Sep 27, 2018 at 13:39
  • @user3439894 given that the OP provided a partial attempt in AppleScript I think CJK is right here. While I think both approaches work I think this is the better answer. +1 here
    – ʀ2ᴅ2
    Commented Sep 27, 2018 at 15:19
  • I must agree with @user3439894, it is total overkill and not clear what extras does it do as compared to his answer.
    – Ruskes
    Commented Sep 27, 2018 at 16:05
  • 1
    @ʀ2ᴅ2 we have the Please avoid extended discussions in comments. so lets leave it at that. Ultimately the OP will decide.
    – Ruskes
    Commented Sep 27, 2018 at 17:51
  • 2
    @CJK This is beautiful. Thank you so much for putting so much effort in. I've been really torn choosing an answer but, sorry to say, I did end up going the bash script route. Not at all your fault though. It's just easier for my baby coding brain to understand that simpler script, which will make things less confusing for me if I need to tweak the process in future. Lovely as your applescript is, I'd be worried about ruining something if I tried adjusting it on my own. Thank you so much for the quick and thorough response. So gratfeful for this community right now.
    – David
    Commented Sep 28, 2018 at 4:23

You must log in to answer this question.

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