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:
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.
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.
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).