Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make a pdf look scanned with macOS automator as a quick action
# This script takes a PDF or list of PDFs and outputs a file(s)
# named <file>_scanned.pdf that looks like it has been scanned
# Requires imagemagic and popper to be installed (brew install imagemagick poppler)
# Accepts: a list of files
# Usage: ./<scriptfilename> pdf1.pdf pdf2.pdf
# To use as a macOS automator quick action you need to:
# - Create a new quick action that receives files or folders
# - Add a shell script
# - Paste in this script
set -e
export PATH=/usr/local/bin:$PATH
for file in "$@"; do
# Split PDF into pages
echo "Splitting $base into separate pages"
pdfseparate "$file" /tmp/fake-scan-split-%04d.pdf
# Loop over the pages
for splitFile in /tmp/fake-scan-split-*.pdf; do
# Slightly rotate page, add a bit of noise and output a flat pdf
convert -density 130 -trim -flatten "$splitFile" -attenuate 0.2 +noise Gaussian -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 8 + 1))" "$splitFileScanned"
echo "Output page $splitFileBase to $splitFileScanned"
# Combine the PDFs, add noise across the entire document, apply sharpening, convert to b&w, soften the blacks slightly
convert -density 130 $(ls -rt /tmp/fake-scan-split-*_scanned.pdf) -attenuate 0.2 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray +level 15%,100% "$base"
echo "PDF re-combined to $base"
# Remove all the temporary PDFs
echo "Cleaning up"
rm /tmp/fake-scan-split-*.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment