Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to convert PDF to JPEG using Preview?

I know there's an existing script available on the internet that converts PDFs to JPEGs (it's here: http://macscripter.net/viewtopic.php?id=25350) BUT, for whatever reason, the quality, even when set to 600dpi, is crap. I get better results simply opening the PDF in Preview then saving as JPEG... even a 150dpi JPG from Preview is higher quality than a 600dpi file from this script I found...


So, what I need help with is creating a applescript that converts PDF's to JPEGs using Preview (or whatever, as long as the quality of the JPEG is good). I have very little scripting experience so help is appreciate.


Ideas?


Thanks.

imac 24, Mac OS X (10.5.5), 4gb ram

Posted on Apr 21, 2011 2:22 PM

Reply
24 replies

Apr 27, 2011 5:03 PM in response to Pierre L.

Pierre L. wrote:


Is there any way to enable it to select multiple PDFs (or an entire folder with PDFs in it)?


The following should do the trick:


set sourceFolder to choose folder

set destinationFolder to POSIX file "/Users/Peter/Pictures/JPEGs" -- just an example; change to fit you needs

tell application "Finder" to set theFiles to files of the sourceFolder whose name extension is "pdf"

repeat with theFile in theFiles

tell application "Preview" to activate

tell application "System Events" to tell process "Preview"

opentheFile

set theName to displayed name of theFile

repeat until (window 1 whose name begins with theName) exists

delay 0.1

end repeat

keystroke "s" using {shift down, command down}

keystroke "d" using {command down}

tell pop up button 2 of group 1 of sheet 1 of window 1

click

clickmenu item "JPEG" of menu 1

end tell

keystrokereturn

if sheet 1 of sheet 1 of window 1 exists then

click button "Replace" of sheet 1 of sheet 1 of window 1

end if

repeat while sheet 1 of window 1 exists

delay 0.1

end repeat

end tell

tell application "Finder"

set {theName, theExtension} to {name, name extension} of theFile-- the PDF file

tell me to set P to offset of theExtension in theName

set theName to text 1 through (P - 1) of theName & "jpg" -- the name of the JPEG file

movefiletheName of desktopto the destinationFolder with replacing

end tell

tell application "Preview" to activate

tell application "System Events" to keystroke "w" using {command down}

end repeat


(I've also made the code somewhat more robust).


Message was edited by: Pierre L.


Hi Pierre--


Thanks for this, but it didn't work. Once I run it it requests for me to select a folder which I do and then it opens a "save as" option ... I save the file to another location, but then it gives me the following error. Ideas to fix this?:


error "System Events got an error: Can’t get sheet 1 of window 1 of process \"Preview\". Invalid index." number -1719 from sheet 1 of window 1 of process "Preview"


It seemed to me that the problem had something to do with preview, so I quit preview altogether and ran the script again. This time it seemed to work... however it only saved the first file within the folder as a jpeg to desktop, and then gave me this error:


error "Finder got an error: AppleEvent handler failed." number -10000



Ideas?

Apr 27, 2011 5:22 PM in response to thebigother

The sourceFolder should be the folder where you keep your PDF files.

The destinationFolder should be the folder where you want to put the JPEG files. It might be the same as the sourceFolder.

Did you replace “/Users/Peter/Pictures/JPEGs” with the path of your destination folder?

I have tested the script many times and it seems to work flawlessly on my MacBook Pro, with Mac OS X 10.6.7.


Try adding one-second delays before the problematic lines of code.


Message was edited by: Pierre L.

Apr 27, 2011 5:26 PM in response to Pierre L.

Pierre L. wrote:


The sourceFolder should be the folder where you keep your PDF files.

The destinationFolder should be the folder where you want to put the JPEG files. It might be the same as the sourceFolder.

Did you replace “/Users/Peter/Pictures/JPEGs” with the path of your destination folder?

I have tested the script many times and it seems to work flawlessly on my MacBook Pro, with Mac OS X 10.6.7.

Ah, oops. Sorry--I made the change as suggested and it works nicely now! Thank you Pierre!

Apr 27, 2011 5:48 PM in response to Frank Caggiano

Frank Caggiano wrote:


Not sure about the error message you got but his line is wrong

set workFlow to POSIX path of workFlow


change it to

set workFlow to quoted form of POSIX path of workFlow


and give it a try


I also put a copy here: http://dl.dropbox.com/u/13002668/pdfTojpg.scpt in case cutting and pasting from the forum messed things up.

Thanks Frank. I think I found a solution with Pierre's script. To be honest, I also prefer to keep just one script rather than relying on multiple scripts + automator action combinations. Just to keep things simple. I much appreciate your awesome input here!

Applescript to convert PDF to JPEG using Preview?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.