Skip to content

Instantly share code, notes, and snippets.

@lestercheung
Created February 10, 2020 07:01
Show Gist options
  • Save lestercheung/b5eae6c11830d7941598ade54006b7b5 to your computer and use it in GitHub Desktop.
Save lestercheung/b5eae6c11830d7941598ade54006b7b5 to your computer and use it in GitHub Desktop.
MacOS folder action to reduce screenshots size automatically
#!/bin/zsh
# This can be used as a folder action on the macOS's desktop to reduce the size of
# most PNG files automatically.
#
# http://about.me/lestercheung
POSTERIZE=0
PNGQUANT=0
[[ -x /usr/local/bin/posterize ]] && POSTERIZE=1
[[ -x /usr/local/bin/pngquant ]] && PNGQUANT=1
if [[ $POSTERIZE -eq 0 ]]; then
cat <<EOF | /usr/bin/osascript
display alert "You need pngquant installed:\nbrew install pngquant "
EOF
fi
if [[ $PNGQUANT -eq 0 ]]; then
cat <<EOF | /usr/bin/osascript
display alert "You need posterize installed. Bringing you to the github repo..."
open location "https://github.com/kornelski/mediancut-posterizer"
EOF
fi
TMPFILE=`mktemp /tmp/convert.XXXXXX`
for f in $*; do
echo $f >> /tmp/log.pngminify
[[ "$f" != *".png" ]] && continue
if [[ POSTERIZE -eq 1 && PNGQUANT -eq 1 ]]; then
echo converting "$f"
/usr/local/bin/posterize -Q 80 "$f" | /usr/local/bin/pngquant - > "$TMPFILE"
if [[ $? -eq 0 ]]; then
if [[ -x /usr/local/bin/trash ]]; then
/usr/local/bin/trash "$f"
else
/bin/rm "$f"
fi
/bin/mv "$TMPFILE" "$f"
fi
fi
done
[[ -f "$TMPFILE" ]] && /bin/rm "$TMPFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment