Search code examples
macosautomationautomatormacos-monterey

Reduce Selected Image File to 700 pixel via Apple Quick Action


I'm new Apple Automator scripting, I create a new Document type (Quick Action) named, reduceTo700

I have it Run Shell Script

for f in "$@"
do
  /usr/bin/sips -Z 700 "$f" --out "${f%.*}.700px.jpg"
  /usr/bin/pbcopy < "${f%.*}.700px.jpg"
done

enter image description here

The logs show green, but I see no file generated.

enter image description here

How can I get this to work ?


Solution

  • Assuming the following:

    • you don't really want to retain any "on disk" output files, and
    • you don't really want to copy multiple files to the clipboard

    you can use this:

    # Pick up and use just first filename received
    f="$1"
    
    # Make temp file to ensure PNG and to ensure we can remove at end 
    tmp="$(/usr/bin/mktemp /tmp/sipsZ700-XXXXXX).png"
    
    # Resize
    /usr/bin/sips -Z 700 "$f" -s format png --out "$tmp"
    
    # Tell Finder to put it on pasteboard
    osascript <<EOF
    tell app "Finder" to set the clipboard to (read ( POSIX file "$tmp" ) as {«class PNGf»})
    EOF
    
    # Clean up
    rm "$tmp"
    

    I hard-coded PNG format because it can support 8/16-bit images and also transparency, so it is arguably more flexible than JPEG.