Skip to content

Instantly share code, notes, and snippets.

@stoph
Created October 14, 2024 18:59
Show Gist options
  • Select an option

  • Save stoph/1611f19a63b9ebb1d9813eee7151b8df to your computer and use it in GitHub Desktop.

Select an option

Save stoph/1611f19a63b9ebb1d9813eee7151b8df to your computer and use it in GitHub Desktop.
Compress PNG Finder Automation
for f in "$@"
do
# Get the base name of the file without the extension
base_name=$(basename "$f" .png)
# Get the directory where the file is located
dir_name=$(dirname "$f")
# Create a backup with the ".org.png" extension
cp "$f" "$dir_name/$base_name.org.png"
# Compress the original image
/opt/homebrew/bin/pngquant --force --ext .png "$f"
done
@stoph
Copy link
Author

stoph commented Oct 15, 2024

Step 1: Install pngquant

1. Open Terminal on your Mac.
2. Install pngquant via Homebrew
brew install pngquant

Step 2: Create an Automator Quick Action

1. Open Automator on your Mac (you can find it using Spotlight or in the Applications folder)
2. Choose New Document
3. In the dialog that appears, select Quick Action
4. At the top of the new workflow, set the following options:
    Workflow receives current: Change this to “image files”
    In: Leave this as “Finder”

Step 3: Add the Shell Script Action

1. In the Library on the left-hand side, search for Run Shell Script.
2. Drag the Run Shell Script action into the workflow pane on the right.
3. In the Run Shell Script action, set the following:
    Pass input: Select “as arguments”
4. Replace the default script with the following pngquant compression script (or the above code to create a backup of the image first)
for f in "$@"
do
  /opt/homebrew/bin/pngquant --force --ext .png "$f"
done

Step 4: Save the Quick Action

1. Go to File → Save
2. Name your Quick Action something like “Compress PNG” (this will be the name that appears when you right-click on files)
3. Click Save

Step 5: Test the Quick Action

1. In Finder, right-click on any PNG image
2. Go to Quick Actions → Compress Image
3. The file will be compressed and the original image will be overwritten with the compressed version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment