You would just need to call from bin a check if a compression exists: try AppleScript/Automater could also be scripted into using a compression utility. If you want something you can run with PNGs and JPGs you could use Trimage. Regarding the compression aspect, if these are PNGs you can use Optipng. You could resample based on height with -resampleHeight. The above code uses tail and cut to trim the return. If you do man sips in the Terminal you can pull all types of info. As mentioned in the comment you could check to see which is longer the width or height and resize from there. That is just an example of what you can do with the width.
If imgWidth ≠ theWidth then do shell script "sips -resampleWidth " & theWidth & " " & imagePos # if not equal to set variable width resize Set imgWidth to (do shell script "sips -g pixelWidth " & " " & imagePos & " " & "| tail -n1 | cut -d' ' -f4") as integer Set imagePos to quoted form of POSIX path of ((imageDir as text) & contents of theImage) Set the imageList to list folder imageDir without invisibles Set imageDir to choose folder with prompt "Please select directory." As mentioned, there is sips, a working example based on pulling a directory of images and stepping through the list with a resize if the width isn't 800px: property theWidth : 800
RESIZE IMAGES USING AUTOMATOR MAC HOW TO
I'm a little confused with the approach on how to properly compress the images you want to work with.