Export PDF as JPEG Macro (v11.0.2)

I'm trying to automate saving a PDF as a JPEG in Preview.

The manual way: File > Export > Click Format menu > select JPEG > press Save button.

I've tried to automate this in attached macro, but it does not work. Wondering if AppleScript can save the PDF as a JPEG?

Export PDF as JPEG.kmmacros (3.3 KB)

Doable with KM actions, just drive the dialog from the keyboard. Pseudocode for the macro:

select menu item File->Export...
repeat 7 times -- adjust to suit if different on your system
   keystroke Tab
end repeat
keystroke J
keystroke Return

If you want to use AppleScript, the trick is that you can't explicitly set the export format (regardless of what Preview's AppleScript dictionary says). But Preview will infer the format from the file name extension of the export path. So to export the frontmost image as a JPEG to the same folder as the frontmost image:

tell application "Preview"
	set AppleScript's text item delimiters to "."
	set thePath to every text item of (get path of document 1)
	set item -1 of thePath to "jpg"
	set thePath to thePath as text
	save document 1 in POSIX file thePath
end tell

You can also do this without touching Preview or AppleScript, using a built-in macOS unix utility called sips. The format of the command is pretty simple:

sips -s format jpeg -s formatOptions 80 "Beach party.tiff" --out "Beach party.jpg"

This would convert the specified TIFF image in the current directory to a JPEG at 80% quality.

Here's a basic macro that lets you set the JPG quality level and output folder, then lets you select a folder of images to convert (non image files in the folder will be ignored). The converted images will be in the output folder, leaving the originals unchanged.

Download Macro(s): JPG converter.kmmacros (8.4 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.6.1
  • Keyboard Maestro v11.0.3

-rob.

Downside of sips when converting a PDF to JPEG is that it will only convert the first page of a multipage document. Both the Preview and AppleScript methods convert the current page.

Yikes, I totally missed the PDF part of that. "Never mind!" :slight_smile:

Edit: If you have ImageMagick installed via HomeBrew or MacPorts, you can handle PDFs with convert:

convert -density 300 input.pdf -quality 100 output.jpg

This worked quite well for a PDF I just tested it with, outputting one JPG image per PDF page.

-rob.

1 Like

This macro uses convert to convert PDFs to JPGs. It works just like the more-generic version above, but only handles PDFs. You'll need convert, of course, and the macro tries to be smart about finding it if installed via Homebrew or MacPorts (and provides a way to list your own path if installed via another method).

Download Macro(s): PDF converter.kmmacros (18 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.6.1
  • Keyboard Maestro v11.0.3

It worked well in my testing, and it's reasonably quick, though working through long PDF files takes a bit of time.

-rob.

1 Like