Automator: Service to move files into new subfolders based on file extension *within any selected folder*
-
26-04-2021 - |
سؤال
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:
- Create separate folders for Raw, JPG, and Video within selected folder.
- 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:
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.
المحلول
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
نصائح أخرى
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:
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: