Skip to content

Instantly share code, notes, and snippets.

@jrk
Last active April 30, 2024 16:10
Show Gist options
  • Save jrk/7eb26c9a868039c70bb9 to your computer and use it in GitHub Desktop.
Save jrk/7eb26c9a868039c70bb9 to your computer and use it in GitHub Desktop.
Executable script to run Quartz filters on a PDF file from the shell

This is a simple, should-be-drop-in replacement for the old /System/Library/Printers/Libraries/quartzfilter utility which went away circa OS X 10.8.

Usage:

chmod +x quartzfilter.js
./quartzfilter.js infile.pdf filter.qsfilter outfile.pdf

e.g.,

./quartzfilter.js my-big-pdf.pdf "/System/Library/Filters/Reduce File Size.qfilter" my-less-big-pdf.pdf

Built using direct calls into Quartz.framework from JavaScript for Automation (JSX).

#!/usr/bin/env osascript -l JavaScript
ObjC.import("Foundation")
ObjC.import("Quartz")
ObjC.import("stdlib")
function save(inpath, filterpath, outpath) {
inURL = $.NSURL.fileURLWithPath(inpath)
inPDF = $.PDFDocument.alloc.initWithURL(inURL)
filterURL = $.NSURL.fileURLWithPath(filterpath)
filter = $.QuartzFilter.quartzFilterWithURL(filterURL)
inPDF.writeToFileWithOptions(outpath, $({"QuartzFilter": filter}))
}
function run(argv) {
if (argv.length != 3) {
console.log("Usage: quartzfilter.js inPDF qsfilter outPDF")
$.exit(-1)
}
save(argv[0], argv[1], argv[2])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment