I don't know much at all about shell scripting, but I managed to cobble together a workflow that actually works.
theFile="{query}"
theImage=$(basename "${theFile}")
fileName="${theImage%.*}"
outputFile=$"$HOME/Desktop/$fileName.jpg"
sips -s format jpeg -s formatOptions 100 "${theFile}" --out "${outputFile}"
open -a ImageOptim "${outputFile}"
I'm using it as a File Action, so select a PNG image, run the workflow and it converts it to JPG, then runs it though ImageOptim.
This works when selecting single images. but doesn't work if I pass it multiple PNGs. In the File Action part of the workflow I checked 'Accept multiple files', so I'm guessing I need to alter the script to make it work? In the Run Script part of the workflow I tried it with 'running instances' set to Sequentially and then Concurrently, but that didn't make any difference.
Also, I wanted to show the name of the image in a notification. I tried {query} but that shows the full path to the original image, and also tried ${fileName} (and variations of it) but that didn't work either.
Any help would be appreciated ๐