Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save orkoden/030afd08ae3b32a00fddc624be6fc88d to your computer and use it in GitHub Desktop.

Select an option

Save orkoden/030afd08ae3b32a00fddc624be6fc88d to your computer and use it in GitHub Desktop.
Automatically compress screenshots losslessly on macOS using Folder Actions, ImageOptim, and Automator
#!/bin/zsh
open -g -a ImageOptim $@
<<instructions
# Automatically compress screenshots losslessly on macOS
Reduce file size of your screenshots by 30% to 80% with this setup.
Tested on macOS 26.3. Should work on every Mac OS X 10.5 or newer.
## Install and Run ImageOptim
1. Download [ImageOptim](https://imageoptim.com/mac).
2. Doubleclick to unpack *ImageOptim.tbz2*
3. (Optional) Move *ImageOptim* to *Applications* (Deutsch: *Programme*).
4. Doubleclick to open *ImageOptim*
5. Select **Allow** to please Gatekeeper
6. Quit *ImageOptim*
## Create Workflow in Automator
### 1. Folder Action
1. Open *Applications:Automator*
2. > **File** > **New**
3. Click **Folder Action** then **Select**
4. On the top right **Choose folder** > **Other**
5. Select folder *Desktop* (Deutsch: *Schreibtisch*)
### 2. Add Filter for Screenshots
6. Search for action **Finder filter objects** and drag it to the right.
7. Add `+` condition **Type is Image**
8. Add `+` condition **Name begins with** *Screenshot* (Deutsch: *Bildschirmfoto*).
### 3. Add Shell Script
9. Search for action **Run shell script** and drag it to the right.
10. Select shell **/bin/zsh** and for input choose **As arguments**
11. Delete everything from the text field
12. Copy and paste this whole file (or only the second line) into the text field
13. Save under a memorable name like *OptimizeScreenshots*
### 4. Quit Automator
## Usage
1. Take a new screenshot by pressing `cmd`+`shift`+`3` keys.
2. Wait about 10 seconds.
3. ImageOptim opens in the background and starts optimizing.
4. Watch ImageOptim work and smile.
### Troubleshooting
- Open ImageOptim at least once.
- Make sure to select shell **/bin/zsh** and for input choose **As arguments**.
- Change **Name begins with** to your language. (Deutsch: *Bildschirmfoto*)
### Uninstall
#### Finder
1. > **Go to** > **Go to folder** *~/Library/Workflows/Applications/Folder Actions*
2. Delete *OptimizeScreenshots*
#### Terminal
rm -r '~/Library/Workflows/Applications/Folder Actions/OptimizeScreenshots.workflow'
## How does this work?
### Folder Actions
[Folder Actions](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/WatchFolders.html#//apple_ref/doc/uid/TP40016239-CH39-SW1) allow you to run programs, when files are added or removed.
Shortcuts does not support folder actions as of macOS 26, only [ Automator](https://leopard-adc.pepas.com/documentation/AppleApplications/Conceptual/AutomatorConcepts/Articles/ShellScriptActions.html) and [Apple Script](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html)
### Shell Script
`#!/bin/zsh` Use the ZSH shell.
`open` Open file, folder, or application.
`-g` Do not bring the application to the foreground.
`-a ImageOptim` Open application named ImageOptim.
`$@` The list of files passed by Automator as parameter.
See also [ImageOptim on the command line](https://imageoptim.com/command-line.html).
## I want more options!
Use the command line tool [imageoptim-cli](https:/jamiemason.github.io/ImageOptim-CLI/).
It allows you to pass more parameters for color, alpha, speed.
If installed via homebrew, call it like this:
/opt/homebrew/bin/imageoptim --speed 3 $@
instructions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment