From PDF document to JPEG images in AppleScript

How can I extract JPEG images from a PDF file using AppleScript? Is there a command such as saveJpegFromPage(PDFstream, numberOfPage)?


Short answer: you don’t in straight AppleScript (because there isn’t such a command) – but you can sidestep the issue by using the poppler shell utility package inside AppleScript.

Long answer: AppleScript’s default abilities (as defined by Standard Additions) and the dictionaries provided by the OS and its included applications do not include PDF processing. Tellingly, OS X’ very own PDF application, Preview.app, was not even scriptable for most of its existence.

As to third party apps, Adobe’s Acrobat X Pro might be able to do this (I can’t check, as the price tag is altogether too hefty for me, and there is no trial version for OS X), but of the two best known affordable scriptable third party PDF apps, Skim and PDFPen, neither’s AS dictionary contains commands for processing images in PDF files.

However, as is often the case, the shell provides an alternative. The xpdf package contains a utility called pdfimages which will do what you are after. The command

pdfimages -j -f 1 -l 1 "/path/to/my file.pdf" "/path/to/folder for images"

will save all images on page 1 of /path/to/my file.pdf to the folder /path/to/folder for images in JPEG format (refer to man pdfimages for an explanation of all options). Note this is taken from poppler’s pdfimages man page, which is an evolution of the original xpdf package. Your best bet for installing poppler probably is the homebrew package manager (brew install poppler).

Once you have the package on your system, all you have to do to use its abilities inside AppleScript is wrap it in a do shell script command, along the lines of

do shell script "'path/to/pdfimages' -j -f " & pageNum & " -l " & pageNum & " '" & (POSIX path of myPDF) & "' '" & (POSIX path of targetFolder) & "'"

and you should be set.