The link to your workflow file didn't work, but it's ok—thanks for trying to share it anyway, but, in this instance, I can work without it, as I'm actually going to do it from scratch:
Depending how your workflow will be setup to receive the input, you can remove the Get Specified Finder Items action and allow the workflow to run as a Service, for example, whose input will be the folder that contains images to be processed. As you said it was your Downloads folder that contains the images, I felt it was unlikely that you'd be setting up the workflow to work as a Folder Action.
For testing purposes, you can keep the Get Specified Finder Items action and replace the Example folder I've put in there with your Downloads folder.
Here's the code from the Run AppleScript action for you to copy/paste:
on run {input, parameters}
# input will be the folder containing the images, e.g. the Downloads folder
set [here] to the input
set suffix to "preview.jpg"
set my text item delimiters to {pi, space & suffix}
tell application "Finder"
set jpgs to the name of every file in here whose name ends with the suffix
repeat with jpg in jpgs
set this to the text items of jpg as text
set there to (a reference to the folder named this in here)
if not (there exists) then set there ¬
to make new folder in here ¬
with properties {name:this}
move the file named jpg in here to there
end repeat
end tell
end run
It matters not whether the destination folders exist: if they do, the image will be moved into the appropriate one; for those that don't, the folder is created before the image is moved.
Update In Response To Comments
① Sometimes I have a string between "preview" and extension .jpg
or .png
② I'd like to also move the completed folders to a new folder /Users/sebba/BLENDER/BLENDS
Given the variability of your filenames that you've now disclosed, I think it's easier to use a shell script instead of an AppleScript to process the files.
Delete the Run AppleScript action and insert a Run Shell Script action, with the following options: Shell: bin/bash
, Pass input: as arguments
.
Delete any sample code that appears in the editing field and enter this code:
cd "$1"
folder=()
while read -r file
do
folder+=("$(egrep -io '^.+ \d{3}' <<<"$file")")
[[ -d "${folder[@]: -1}" ]] || mkdir "${folder[@]: -1}"
mv -v "$file" "${folder[@]: -1}"
done <<<"$( ls | egrep -i '^.+ \d{3} preview.*\.(jpg|png)' )"
mv -v "${folder[@]}" "/Users/sebba/BLENDER/BLENDS"
This uses a regular expression match to pick out filenames that:
- Begin with absolutely anything (but not nothing); then
- Follow with a space, precisely three digits, a space, then the word "preview"; then
- Follow with absolutey anything (including nothing); then
- End with ".jpg" or ".png".
Therefore, it will move the following files to the given folder (the folder is created if it doesn't exist, and files with the same name are overwritten):
My image_file 001 preview.foo bar.jpg
→ My image_file 001/
004 2 001 preview.png
→ 004 2 001/
But the following files won't be affected:
001 preview.png
My image_file 01 preview.jpg