Here’s a way to think about tackling it, and I’m sure it could be a little more efficient if you processed it based on the file being processed in Hazel, but I’m imagining the volumes are such that it won’t make any practical difference in the amount of time to process. So, this is presented on the basis that you run it each time a new file is added, and it will simply process the entire directory each time.
I took this approach because Hazel is based around processing a single file at a time, but you want to process sets of files in concert rather than individual files.
For the Hazel rule, process any file by running a shell script - you can make it stand alone or embedded as you see fit and matches how you manage your scripts.
Here’s my test folder:
In it I have four JPG files, where one has no matched pair (bar.jpg
), one has a single matching EPUB (foo.jpg
), one has a single matching MOBI (baz.jpg
), and for good measure, one has both a matching EPUB and a matching MOBI (qua.jpg
). I assume that you could get the latter if you added the EPUB and MOBI files first and then the JPG afterwards as it is the JPG that acts as the trigger. For info, the inverse of this scenario where the JPG is added first is why I didn’t make the Hazel rule match only on JPG files - i.e. the matching files may come in later, and before another JPG file arrives to trigger anything.
The logic is in the shell script. So that you can test things out first before using it, the script is purposefully dummied so that it will not do anything … I’ll explain.
- Every file move command I have prefixed with
echo
, so that it will just output to the command line what it would do. When yoiu are ready to use it, simply replace all of the echo mv
strings with just mv
.
- The script should be customised to meet your particular requirements through the settings:
-
dirSrc
= the path to the folder you are processing in Hazel.
-
dirMatch
= the path to the folder where you want to move matched files too
-
extMain
= the file extension of the must have file (e.g. JPG)
-
extAlt1
= a file extension of an optional match file (e.g. EPUB)
-
extAlt2
= a file extension of an optional match file (e.g. MOBI)
Make sure you modify the settings in the script below and modify the dummied moves when you are ready to use it.
#!/bin/zsh
# Settings
dirSrc="/Users/stephen/Temp/check"
dirMatch="/Users/stephen/Temp/match"
extMain="jpg"
extAlt1="epub"
extAlt2="mobi"
# Change to the directory we want to process
cd $dirSrc
# Check all files in the directory
for file in *.$extMain
do
echo checking "\"$file\""...
# Move Alt1 file if it exists, along with the original
if [[ -e ${file%%$extMain}$extAlt1 ]]; then
echo mv "$dirSrc/${file%%$extMain}$extAlt1" "$dirMatch/${file%%$extMain}$extAlt1"
echo mv "$dirSrc/$file" "$dirMatch/$file"
fi
# Move Alt2 file if it exists, along with the original if that exists
if [[ -e ${file%%$extMain}$extAlt2 ]]; then
echo mv "$dirSrc/${file%%$extMain}$extAlt2" "$dirMatch/${file%%$extMain}$extAlt2"
# Only try and move the original if that is still there
if [ -f $file ]; then
echo mv "$dirSrc/$file" "$dirMatch/$file"
fi
fi
echo
done
If I run this as is on my system, with the files as above, I get the following output:
checking "bar.jpg"...
checking "baz.jpg"...
mv /Users/stephen/Temp/check/baz.mobi /Users/stephen/Temp/match/baz.mobi
mv /Users/stephen/Temp/check/baz.jpg /Users/stephen/Temp/match/baz.jpg
checking "foo.jpg"...
mv /Users/stephen/Temp/check/foo.epub /Users/stephen/Temp/match/foo.epub
mv /Users/stephen/Temp/check/foo.jpg /Users/stephen/Temp/match/foo.jpg
checking "quz.jpg"...
mv /Users/stephen/Temp/check/quz.epub /Users/stephen/Temp/match/quz.epub
mv /Users/stephen/Temp/check/quz.jpg /Users/stephen/Temp/match/quz.jpg
mv /Users/stephen/Temp/check/quz.mobi /Users/stephen/Temp/match/quz.mobi
mv /Users/stephen/Temp/check/quz.jpg /Users/stephen/Temp/match/quz.jpg
Now, if I modify the script to do the moves rather than just echoing the commands and walk through the results.
Starting with the source and match folder listings of this:
Then running the script standalone, the output is this:
checking "bar.jpg"...
checking "baz.jpg"...
checking "foo.jpg"...
checking "quz.jpg"...
The resulting listings become:
All of the matching files have been moved.
Hopefully, that all makes sense, and meets your needs.