AppleScript – Translate Automator Workflow to AppleScript

applescriptautomatorgraphics

Workflow

What I want to do is select a bunch of images with varying dimensions and uniformly apply a percentage scale to resize them. The workflow comes very close to that. But I cant get more specific to the nearest tenths. By pixels it only takes length into consideration whereas I want the width. So i figured as long as it is in applescript I can easily specify the scale I want. I intend to run the script as a service.

Best Answer

You can use a "Run Shell Script" action and sips to downscale your pictures to any value. Add a "Run Shell Script" action, make sure the input method is set to "stdin" (should be the default) and insert the following script

SCALEFACTOR=500
while IFS= read file; do
    sips --resampleWidth $(($(sips -g pixelWidth "$file" |
                                  sed -n -e '/pixelWidth/s/.*pixelWidth: //p')
                            * $SCALEFACTOR / 1000)) "$file" --out "${file%/*}/resized-${file##*/}"
done

Shell arithmetic only supports integer values, so you'll have to specify the scale factor in thousands (e.g. 424 for 42.4%)