Created
December 4, 2025 08:14
-
-
Save thaikolja/baa07621a8ba188ebb7bfad6cb4a7110 to your computer and use it in GitHub Desktop.
For macOS Quick Action in "Automator": Shell Script to Quickly Format image(s) into .webp, .avif, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # !/bin/bash | |
| # | |
| # Script to convert images to a selected format using ImageMagick's mogrify. | |
| # It handles existing files in the same format by renaming them to avoid overwriting. | |
| # | |
| # USAGE: `./convert.sh image1.jpg image2.png` etc. (when `FORMAT="webp"`) | |
| # OUTPUT: `image1.webp`, `image2.webp` in the same directories as the input images | |
| # | |
| # To be used as copy-paste script for macOS Automator app to create | |
| # a Quick Action that converts selected images in Finder to webp format. | |
| # | |
| # @author Kolja Nolte | |
| # @email [email protected] | |
| # @date 2025-12-03 | |
| # @license MIT | |
| # @version 1.0.0 | |
| # Path to the mogrify binary (ImageMagick) | |
| MOGRIFY="/usr/local/bin/mogrify" | |
| # Desired output format | |
| FORMAT="webp" | |
| # If the hard-coded mogrify is not executable | |
| if [ ! -x "$MOGRIFY" ]; then | |
| # Check if mogrify is available in PATH | |
| if command -v mogrify >/dev/null 2>&1; then | |
| # Use the mogrify found in PATH | |
| MOGRIFY="$(command -v mogrify)" | |
| else | |
| # Print message if mogrify is not installed | |
| echo 'ImageMagick is not installed.' | |
| # Exit with failure status if mogrify is missing | |
| exit 1 | |
| fi | |
| fi | |
| # Loop over all script arguments (images) | |
| for IMAGE in "$@"; do | |
| # If the argument is not a regular file | |
| if [ ! -f "$IMAGE" ]; then | |
| # Inform the user and skip this argument | |
| echo "Skipping '$IMAGE': not a file" | |
| # Skip to the next argument | |
| continue | |
| fi | |
| # Directory containing the image | |
| IMAGE_PATH=$(dirname "$IMAGE") | |
| # Filename part of the image | |
| IMAGE_FILENAME=$(basename "$IMAGE") | |
| # Filename without extension | |
| IMAGE_BASENAME="${IMAGE_FILENAME%.*}" | |
| # Default output filename | |
| DEFAULT_OUTPUT="${IMAGE_PATH}/${IMAGE_BASENAME}.${FORMAT}" | |
| # Initialize final output path to default | |
| OUTPUT_PATH="$DEFAULT_OUTPUT" | |
| # If a file with the default output name already exists | |
| if [ -f "$DEFAULT_OUTPUT" ]; then | |
| # Start numbering suffixes at 1 | |
| SUFFIX=1 | |
| # Candidate unique filename | |
| OUTPUT_PATH="${IMAGE_PATH}/${IMAGE_BASENAME}-${SUFFIX}.${FORMAT}" | |
| # While the candidate filename already exists | |
| while [ -f "$OUTPUT_PATH" ]; do | |
| # Increment the suffix to get a new candidate | |
| SUFFIX=$((SUFFIX + 1)) | |
| # Update the candidate filename | |
| OUTPUT_PATH="${IMAGE_PATH}/${IMAGE_BASENAME}-${SUFFIX}.${FORMAT}" | |
| done | |
| # Move existing default $FORMAT to the unique filename to avoid overwrite | |
| mv "$DEFAULT_OUTPUT" "$OUTPUT_PATH" | |
| fi | |
| # Print the path that will be created for the converted image | |
| echo "$DEFAULT_OUTPUT" | |
| # Run mogrify to create $FORMAT in the image directory | |
| "$MOGRIFY" -format ${FORMAT} "$IMAGE" -path "$IMAGE_PATH" | |
| # If conversion did not produce the expected default output file | |
| if [ ! -f "$DEFAULT_OUTPUT" ]; then | |
| # Inform the user about the failure | |
| echo "Failed to create '$DEFAULT_OUTPUT' for '$IMAGE'." | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment