You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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 21, 2011 3:38 PM in response to Frank Caggiano

Thanks Frank. Have been tinkering with this automator variable and got it to convert the PDF to JPG properly... however, it renames the file with a random name. I tried adding the "rename finder items > make sequential action" to it but it only adds a sequential number to the random name. I need the name of the file to remain constant... so when I convert a file named for example "test.pdf", it must come out as a jpeg named "test.jpg". Ideas?

Apr 21, 2011 7:21 PM in response to thebigother

thebigother wrote:


I tried adding the "rename finder items > make sequential action" to it but it only adds a sequential number to the random name. I need the name of the file to remain constant...


To rename converted items while also making them sequential, try selecting the new name radio button in Automator's Rename Finder Items action. The text field next to new name can either be pre-populated with the original PDF's name, or you can check Show Action When Run under Options so that the items can be renamed on the fly each time the workflow is run.


This example workflow worked for me in Mac OS 10.4.11, using Automator 1.0.5:


1) Ask for Finder Items -- Type: Files. This action was used to select the original PDF file.


2) Render PDF Pages as Images -- Format: JPEG image


3) Copy Finder Items -- To: Desktop, e.g.


4) Rename Finder Items -- Click the popup button and choose Make Sequential. Select the new name radio button and enter the desired name, or leave the text field blank and check Show Action When Run under Options so that the new files can be renamed when a dialog appears at runtime.


Good luck, hope this helps.

Apr 21, 2011 8:24 PM in response to thebigother

Good news and bad news. The good news is it is doable the bad news is it isn't as easy as it should be.


The sticking point is naming the new jpg's with the old pdf filename with the jpg extension of course. There are a couple of different ways to approach this but it really depends on just what it is your looking to do. For example how often will this happen? Is it a one time thing or ongoing? How many files and is each pdf a single page or will they be multiple pages?


regards

Apr 22, 2011 2:57 PM in response to thebigother

Not sure if you're still interested in this. I have something which works, it's still rough, but it works. If you are still interested post back and answer the questions to my previous post.


-- choose PDF file
set input to choose file with multiple selections allowed 

repeat with sourceFile in input
          set bName to do shell script "basename " & quoted form of POSIX path of sourceFile
          set bName to do shell script "echo " & bName & "|sed s/.pdf//"

          set res to do shell script "automator -i " & quoted form of POSIX path of sourceFile & " /Users/frank/Desktop/pdfjpg.workflow"
          set text item delimiters to "\""
          set fullPath to quoted form of POSIX path of text item 2 of res

          do shell script "mv " & fullPath & space & "/Users/frank/Desktop/" & bName & ".JPG"

end repeat

Basically the applescript runs an Automator action that does the actual conversion. The Applescript manages the filenames. This is the script but with hardcoded in paths. Like I said rough but it shows it can be done.

Apr 23, 2011 8:18 PM in response to Frank Caggiano

Decided to finish this up as an interesting exercise. Hope you find it useful.


One question for the Applescripters here. The Automator action run as a shell script seems to return a list of filename aliases but the return value while looking like a list didn't behave as a list. I managed to rip it apart to get to the filepaths and the script works but it seems really kludgey . So my question is given the return of the Automator run as a do shell script what is a more correct way to handle it?



Convert PDF to JPG

(*


© 2011 Frank Caggiano

GNU Public License


Convert pfd files to jpg images.

The converted JPG files wil have the name of the original PDF files with the extension changed to JPG.


The actual conversion uses the Automator action Render PDF Pages as Images.

The user needs to create an Automator action with Render Pages as Images as the single action.

Set the parameters for the conversion in the action.


NOTE: this script assumes you're converting to jpg files If you select another output format in the action this script will

need to be modified.

*)


-- choose PDF files

try

set sourceFiles to choose filewith prompt "Select PDF files" of type {"com.adobe.pdf"} with multiple selections allowed

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try


-- choose destination folder

try

set destFolder to choose folder with prompt "Select Destination Folder"

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try

set destFolder to quoted form of POSIX path of destFolder


-- select workflow

try

set workFlow to choose file with prompt "Select Work Flow" of type {"com.apple.automator-workflow"}

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try

set workFlow to POSIX path of workFlow


repeat with sourceFile in sourceFiles


-- get base name of the source file

set bName to do shell script "basename " & quoted form of POSIX path of sourceFile



-- Strip off the extension

set text item delimiters to "."

set bName to text item 1 of bName


try

set res to do shell script "automator -i " & quoted form of POSIX path of sourceFile & space & workFlow

on error msg number n

error msg & space & n

end try



-- Seems strange to do it this way but it works

set text item delimiters to "\""

set theList to text items of res


(*

We go through the list of converted files. If there is more then one then the second and subsequent files

will get and integer added to the name to avoid conflict.

*)


set cnt to 0

repeat with convertedFile in theList

if convertedFile does not contain "alias" and convertedFile does not contain "}" then

set fullPath to quoted form of POSIX path of convertedFile

if cnt ≠ 0 then

do shell script "mv " & fullPath & space & destFolder & bName & "_" & cnt & ".JPG"

else

do shell script "mv " & fullPath & space & destFolder & bName & ".JPG"

end if

set cnt to cnt + 1

end if

end repeat

end repeat

Apr 26, 2011 5:04 PM in response to thebigother

So, what I need help with is creating a applescript that converts PDF's to JPEGs using Preview…


Maybe the following script can do what you are asking for:


set theFile to choose fileof type "pdf"

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

end tell

tell application "Finder"

set {theName, theExtension} to {name, name extension} of theFile

tell me to set P to offset of theExtension in theName

set theName to text 1 through (P - 1) of theName & "jpg"

repeat until file theName of desktop exists

delay 0.1

end repeat

movefiletheName of desktoptocontainer of theFile with replacing

end tell

tell application "Preview" to activate

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


Message was edited by: Pierre L.

Apr 27, 2011 10:42 AM in response to Andrew99

Andrew99 wrote:


To rename converted items while also making them sequential, try selecting the new name radio button in Automator's Rename Finder Items action. The text field next to new name can either be pre-populated with the original PDF's name, or you can check Show Action When Run under Options so that the items can be renamed on the fly each time the workflow is run.


This example workflow worked for me in Mac OS 10.4.11, using Automator 1.0.5:


1) Ask for Finder Items -- Type: Files. This action was used to select the original PDF file.


2) Render PDF Pages as Images -- Format: JPEG image


3) Copy Finder Items -- To: Desktop, e.g.


4) Rename Finder Items -- Click the popup button and choose Make Sequential. Select the new name radio button and enter the desired name, or leave the text field blank and check Show Action When Run under Options so that the new files can be renamed when a dialog appears at runtime.


Good luck, hope this helps.


Hi Andrew--


Thanks. This came close. However, the files I'm dealing with all have very unique file names with not necessarily sequential numbers (ie, company-dc-14avenue-floorplan-old-3a.pdf, company-dc-14avenue-floorplan-old-4d.pdf, company-dc-14avenue-floorplan-old-4e.pdf, etc) and I can't change these names to a new name (at least not in this phase).


I need the script to keep the name exactly as is unfortunately. Thanks for the post.

Apr 27, 2011 10:46 AM in response to Frank Caggiano

Frank Caggiano wrote:


The sticking point is naming the new jpg's with the old pdf filename with the jpg extension of course. There are a couple of different ways to approach this but it really depends on just what it is your looking to do. For example how often will this happen? Is it a one time thing or ongoing? How many files and is each pdf a single page or will they be multiple pages?


regards


This will be ongoing over 1.5-2 years (ie, I will be receiving PDF files that need to be converted to JPG while keeping the same name). I will be getting these files in batches about every week to two weeks. So I'll be using this script a lot (if we can even get one to work that is). Each PDF is a single page.


Thanks.

Apr 27, 2011 10:53 AM in response to Frank Caggiano

Frank Caggiano wrote:


Decided to finish this up as an interesting exercise. Hope you find it useful.


One question for the Applescripters here. The Automator action run as a shell script seems to return a list of filename aliases but the return value while looking like a list didn't behave as a list. I managed to rip it apart to get to the filepaths and the script works but it seems really kludgey . So my question is given the return of the Automator run as a do shell script what is a more correct way to handle it?



Convert PDF to JPG

(*


© 2011 Frank Caggiano

GNU Public License


Convert pfd files to jpg images.

The converted JPG files wil have the name of the original PDF files with the extension changed to JPG.


The actual conversion uses the Automator action Render PDF Pages as Images.

The user needs to create an Automator action with Render Pages as Images as the single action.

Set the parameters for the conversion in the action.


NOTE: this script assumes you're converting to jpg files If you select another output format in the action this script will

need to be modified.

*)


-- choose PDF files

try

set sourceFiles to choose filewith prompt "Select PDF files" of type {"com.adobe.pdf"} with multiple selections allowed

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try


-- choose destination folder

try

set destFolder to choose folder with prompt "Select Destination Folder"

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try

set destFolder to quoted form of POSIX path of destFolder


-- select workflow

try

set workFlow to choose file with prompt "Select Work Flow" of type {"com.apple.automator-workflow"}

on error msg number n

if n ≠ -128 then

error "Unknow error: " & msg & space & n

else


quit

end if

end try

set workFlow to POSIX path of workFlow


repeat with sourceFile in sourceFiles


-- get base name of the source file

set bName to do shell script "basename " & quoted form of POSIX path of sourceFile



-- Strip off the extension

set text item delimiters to "."

set bName to text item 1 of bName


try

set res to do shell script "automator -i " & quoted form of POSIX path of sourceFile & space & workFlow

on error msg number n

error msg & space & n

end try



-- Seems strange to do it this way but it works

set text item delimiters to "\""

set theList to text items of res


(*

We go through the list of converted files. If there is more then one then the second and subsequent files

will get and integer added to the name to avoid conflict.

*)


set cnt to 0

repeat with convertedFile in theList

if convertedFile does not contain "alias" and convertedFile does not contain "}" then

set fullPath to quoted form of POSIX path of convertedFile

if cnt ≠ 0 then

do shell script "mv " & fullPath & space & destFolder & bName & "_" & cnt & ".JPG"

else

do shell script "mv " & fullPath & space & destFolder & bName & ".JPG"

end if

set cnt to cnt + 1

end if

end repeat

end repeat

Hi Frank--


I tried the script you wrote and created the Automator workflow with the single action as requested, saved it to the desktop then selected it when your script's dialogue requested it. However, at that point your script game me the following error. Ideas? I did save the workflow as a workflow and I selected it directly, so I'm not sure why it thinks that the "workflow file does not exist" ? :


error "The workflow file does not exist. 255" number -2700 from «script» to item

Apr 27, 2011 10:59 AM in response to Pierre L.

Pierre L. wrote:


So, what I need help with is creating a applescript that converts PDF's to JPEGs using Preview…


Maybe the following script can do what you are asking for:


set theFile to choose fileof type "pdf"

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

end tell

tell application "Finder"

set {theName, theExtension} to {name, name extension} of theFile

tell me to set P to offset of theExtension in theName

set theName to text 1 through (P - 1) of theName & "jpg"

repeat until file theName of desktop exists

delay 0.1

end repeat

movefiletheName of desktoptocontainer of theFile with replacing

end tell

tell application "Preview" to activate

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


Message was edited by: Pierre L.

Hi Pierre--


This works well, however it only let's me select one file at a time. Is there any way to enable it to select multiple PDFs (or an entire folder with PDFs in it)?


If it can do that, this is a winner!


Thanks.

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

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 the path 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

tell me to set P to offset of theExtension in theName

set theName to text 1 through (P - 1) of theName & "jpg"

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.

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 Account.