#!/bin/bash # Add Homebrew paths to PATH (for both Intel and Apple Silicon Macs) export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH" # Check if ghostscript is installed if ! command -v gs &> /dev/null; then # Check if Homebrew is installed if ! command -v brew &> /dev/null; then result=$(osascript <&1) if [[ $result == *"Cancel"* ]] || [[ $result == *"cancel"* ]]; then continue fi rm "$output_file" fi # Get file sizes for comparison input_size=$(stat -f%z "$input_file") input_size_mb=$(printf "%.2f" $(echo "$input_size / 1048576" | bc -l)) # Compress PDF using ghostscript with high quality settings gs -q -dNOPAUSE -dBATCH -dSAFER \ -sDEVICE=pdfwrite \ -dCompatibilityLevel=1.4 \ -dPDFSETTINGS=/ebook \ -dEmbedAllFonts=true \ -dSubsetFonts=true \ -dColorImageDownsampleType=/Bicubic \ -dColorImageResolution=150 \ -dGrayImageDownsampleType=/Bicubic \ -dGrayImageResolution=150 \ -dMonoImageDownsampleType=/Bicubic \ -dMonoImageResolution=150 \ -sOutputFile="$output_file" \ "$input_file" 2>/dev/null # Check if compression was successful if [ -f "$output_file" ] && [ -s "$output_file" ]; then output_size=$(stat -f%z "$output_file") output_size_mb=$(printf "%.2f" $(echo "$output_size / 1048576" | bc -l)) # Calculate reduction (handle division by zero) if [ "$input_size" -gt 0 ]; then reduction=$(printf "%.1f" $(echo "(1 - $output_size / $input_size) * 100" | bc -l)) else reduction="0.0" fi osascript -e "display notification \"Compressed: ${input_size_mb}MB → ${output_size_mb}MB (${reduction}% smaller)\" with title \"PDF Compression Complete\"" else osascript -e 'display dialog "Compression failed. The PDF may be corrupt or protected." with icon caution' # Clean up failed output file [ -f "$output_file" ] && rm "$output_file" fi done