Part of the Automation series
Every episode of Friends with Brews, I need to generate images for the drinks to include in the podcast chapter artwork and (more importantly) to feature on the website. What I create is a square image in PNG format (for reasons I won’t get into here, but more on the image format later) that has a naming scheme that reflects the manufacturer, the drink name, and a UUID to make sure I never accidentally have file naming collisions (although that should never happen anyway in this use case). I used to do this manually, but no longer.
At some recent point in my life, I purchased a copy of Flying Meat’s Retrobatch. I don’t remember why, other than I was manually tweaking images for Friends with Brews already, and I am a very happy customer of Flying Meat’s other product, Acorn. Acorn is the image editor that I was using to get beverage images ready for the site – it’s simple but powerful, and it’s human-friendly, as their tagline “the image editor for humans” attests. Anyway, Retrobatch is all about batch image processing, and I’m pretty sure my thought process was along the lines of getting out of the manual image tweaking business for Friends with Brews.
AppleScript, however, can talk to both the shell and Retrobatch, so I can use it to fire up Retrobatch processing, generate a UUID with a shell command, and finally, rename all the files in the OUT folder to include the UUID before the file extension.
But, wait – there’s more! It turns out macOS natively supports folder events such that you can run a script when something happens in a folder. This means I can have it run my AppleScript, with a slight modification, whenever I drop the images into my IN folder, and it’ll put the formatted images into my OUT folder and rename them with the UUID added.
The trick to this is in adding my AppleScript (slightly modified) to my
~/Library/Scripts/Folder Action Scripts folder. Note the ~ indicating this is in the user folder’s Library folder, not the top level
First the AppleScript modification: A line at the top and a line at the bottom which tell it to only execute the AppleScript when new items are added to the folder:
The full script now looks like this:
For the AppleScript to execute automatically on a folder, first it needs to be located in
~/Library/Scripts/Folder Action Scripts.
Once I had the AppleScript there, I right-clicked my input folder, selected
Services and then
Folder Action Setup.
On the “Folder Action Setup” dialog box, I clicked on the action name on the right to reveal a dropdown with my AppleScript in the list. I chose the AppleScript, made sure “Enable Folder Actions” was checked, and closed the dialog box.
Now I have a folder that I can drop images into, and they automatically get formatted and renamed and placed in my output folder.
There IS one thing to remember about renaming files in a folder that watches for new items: renaming a file will trigger the script again because it appears to Finder to be a new item. That means if you rename an item in the input folder and the script doing the renaming is set to execute whenever new items appear, it’ll run repeatedly on the same file and the name won’t be what you want. This is why I move the files to the output folder before changing the name by adding the UUID to it.
This workflow doesn’t completely remove all manual labor because I still have to download the files, determine whether or not I need to pre-crop so the Retrobatch square crop action won’t remove something I want, and finally maybe add a background using Acorn if the original image background is transparent. I think I can probably get Retrobatch to handle that last part for me… I hope. I haven’t tried yet.
At the start of this post, I said I’d mention image formats again. I just wanted to point out that regardless of what format I save them to my site src folder in, they get optimized and output in a more responsive-friendly size and format. I use PNG because by being consistent I can overcome an interesting Vite feature regarding dynamic image imports and still get the job done.