Hi Yvan and Nigel,
Yvan, I cannot try the skim.app at this location because we cannot install anything. But will test that at home…
Nigel,
I replaced the one line of code and I think I got rid of the askforwidthandheight() handler… And it does allow me set the DPI and bypasses the with and height question, and does allow me to select the pages, and chose a folder to place the jpegs, but then I get the following message:
Sorry, an Error occurred:
The variable width is not defined (-2753)…I think I commented out too much code??? Can you take a look if you get a chance? Thanks!!
babs
-- author: Martin Michel
-- eMail: martin.michel@macscripter.net
-- created: 01.04.2008
-- modified: 01.05.2011
-- version: 0.2
-- tested on:
-- ¢ Mac OS X 10.6.7
-- ¢ Intel based Macs
-- history:
-- version 0.3
-- ¢ now you can specify a custom width and height
-- for the produced JPG images
-- This script will convert dropped PDF files to JPG images.
-- The JPG images are saved in the same folder as the PDF source files.
-- If a JPG file already exists, it won't be replaced.
-- The PDF source files are not modified.
property mytitle : "PDF2JPEG"
-- last entered values
property lastresolution : missing value
property lastwidthandheight : missing value
property lastjpegfolderpath : missing value
property lastpagenumbers : missing value
-- I am called when the user drops Finder items onto the script icon
on open droppeditems
my main(droppeditems)
end open
-- I am called when the user double clicks the script icon
on run
set infomsg to "I am a hungry AppleScript droplet, so please drop a bunch of PDF files onto my icon to convert them to JPEG images."
my dspinfomsg(infomsg)
return
end run
-- I am the main function controlling the script flow
on main(droppeditems)
try
-- searching th dropped items for PDF files
set pdfpaths to my getpdfpaths(droppeditems)
-- no PDF files found :(
if pdfpaths is {} then
set errmsg to "You did not drop any PDF documents onto the script."
my dsperrmsg(errmsg, "--")
return
end if
-- getting the image resolution to be used for the PDF2JPG conversion
set resolution to my askforresolution()
if resolution is missing value then
return
end if
-- getting the custom image width & height for the PDF2JPG conversion
--set {width, height} to my askforwidthandheight()
(*if width is missing value and hieght is missing value then
return
end if*)
-- getting the pages to be processed
set pagenumbers to my askforpagenumbers()
if pagenumbers is missing value then
return
end if
-- where to save the produced JPEG files?
set jpegfolderpath to my askforjpegfolderpath()
if jpegfolderpath is missing value then
return
else
set jpegfolderpath to POSIX path of jpegfolderpath
end if
-- processing the PDF files
repeat with pdfpath in pdfpaths
set {success, errmsg} to my pdf2jpg(pdfpath, resolution, width, height, pagenumbers, jpegfolderpath)
-- omg...
if not success then
set errmsg to "Processing the following PDF failed:" & return & pdfpath & return & return & "Error: " & errmsg
my dsperrmsg(errmsg, "--")
end if
end repeat
-- catching unexpected errors
on error errmsg number errnum
my dsperrmsg(errmsg, errnum)
end try
end main
-- I search the dropped items for PDF files and return a list of unquoted Posix file paths
on getpdfpaths(droppeditems)
set pdfpaths to {}
repeat with droppeditem in droppeditems
set iteminfo to info for droppeditem
if folder of iteminfo is false and name extension of iteminfo is "pdf" then
set pdfpaths to pdfpaths & (POSIX path of droppeditem)
end if
end repeat
return pdfpaths
end getpdfpaths
-- I return the Posix path to the command line tool responsible for the PDF manipulation
on getcltoolpath()
set pyscriptpath to ((path to me) as text) & "Contents:Resources:pdf2jpeg"
return (POSIX path of pyscriptpath)
end getcltoolpath
-- I convert a given PDF file to JPG
-- [PDF file path must be passed as an unquoted Posix path]
on pdf2jpg(pdfpath, resolution, width, height, pagenumbers, jpegfolderpath)
set command to quoted form of (my getcltoolpath()) & " -pdf " & quoted form of pdfpath & " -out " & quoted form of jpegfolderpath & " -dpi " & resolution
-- does the user want to process certain page numbers only?
if pagenumbers is not "" then
set command to command & " -pages " & pagenumbers
end if
-- conversion...
try
set output to do shell script command
on error errmsg number errnum
return {false, errmsg}
end try
-- does the user want to resample the JPG images to a custom width and height?
if width is -99 and height is -99 then
return {true, missing value}
end if
-- if so, then we are using sips to do so...
set outputlines to paragraphs of output
repeat with outputline in outputlines
if outputline ends with ".jpg" then
set command to "/usr/bin/sips --resampleHeight 72 " & quoted form of outputline
try
do shell script command
on error errmsg number errnum
return {false, errmsg}
end try
end if
end repeat
-- success!
return {true, missing value}
end pdf2jpg
-- I ask the user to provide a value for the resolution
on askforresolution()
-- if possible, we provide the last entered value as the default answer
-- otherwise we default to 150 dpi
if lastresolution is missing value then
set defanswer to "150"
else
set defanswer to lastresolution
end if
-- showing the dialog
set msg to "Please enter a resolution to be used for the JPG conversion (72-600):"
try
tell me
display dialog msg default answer defanswer buttons {"Cancel", "Enter"} default button 2 with title mytitle
set dlgresult to result
end tell
on error errmsg number errnum
return missing value
end try
-- verifying the given user input
set resolution to text returned of dlgresult
-- empty input...asking again :)
if resolution is "" then
my askforresolution()
else
try
-- can the input be coerced to an integer?
set resolution to resolution as integer
on error
-- no, it can't...
set errmsg to "The entered resolution is not a number."
my dsperrmsg(errmsg, "--")
my askforresolution()
end try
-- is the given resolution valid?
if resolution > 600 then
-- no, it's to high...
set errmsg to "The entered resolution (" & resolution & ") exceeds the maximum value (600)."
my dsperrmsg(errmsg, "--")
my askforresolution()
else if resolution < 0 then
-- no, it's to low...
set errmsg to "The entered resolution (" & resolution & ") is a negative value."
my dsperrmsg(errmsg, "--")
my askforresolution()
else
set lastresolution to resolution
return resolution
end if
end if
end askforresolution
--I ask the user to provide the width and height to be used for the JPG images
(*on askforwidthandheight()
-- if possible, we provide the last entered value as the default answer
if lastwidthandheight is not missing value then
set defanswer to lastwidthandheight
else
set defanswer to ""
end if
-- showing the dialog
set msg to "Please enter a custom width and height to be used for the JPG conversion:" & return & "(Example: \"800x600\", or provide no value to use the existing width and height of the PDF pages)"
try
tell me
display dialog msg default answer defanswer buttons {"Cancel", "Enter"} default button 2 with title mytitle
set dlgresult to result
end tell
on error errmsg number errnum
return {missing value, missing value}
end try
-- verifying the given user input
set usrinput to text returned of dlgresult
if usrinput is "" then
return {-99, -99}
else
set usrinputparts to my gettxtitems("x", usrinput)
if length of usrinputparts is not 2 then
my askforwidthandheight()
else
set enteredwidth to (item 1 of usrinputparts) as text
set enteredheight to (item 2 of usrinputparts) as text
try
set width to enteredwidth as integer
set height to enteredheight as integer
set lastwidthandheight to (width & "x" & height) as text
return {width, height}
on error
my askforwidthandheight()
end try
end if
end if
end askforwidthandheight *)
-- I ask the user for the page numbers to be processed
on askforpagenumbers()
-- if possible, we provide the last entered value as the default answer
-- otherwise we default to "" -> all pages
if lastpagenumbers is not missing value then
set defanswer to lastpagenumbers
else
set defanswer to ""
end if
-- showing the dialog
set msg to "Which page numbers should be processed?" & return & "Example: 2,3,5,10-12,87"
try
display dialog msg default answer defanswer buttons {"Cancel", "All", "Enter"} default button 3 with title mytitle
set dlgresult to result
on error errmsg number errnum
return missing value
end try
-- analyzing the user input
if button returned of dlgresult is "All" then
return ""
else if button returned of dlgresult is "Enter" then
set pagenumbers to text returned of dlgresult
set lastpagenumbers to pagenumbers
return pagenumbers
else
return missing value
end if
end askforpagenumbers
-- I ask the user to provide a folder in which to save the JPEG files
on askforjpegfolderpath()
-- if possible, we provide the last chosen folder as the default location
-- otherwise we default to the user's desktop
if lastjpegfolderpath is not missing value then
if my itempathexists(lastjpegfolderpath) then
set deflocation to (lastjpegfolderpath as alias)
else
set deflocation to (path to desktop)
end if
else
set deflocation to (path to desktop from user domain)
end if
-- showing the choose folder panel
try
set msg to "Please choose a folder to save the created JPEG files:"
set chosenfolder to choose folder with prompt msg default location deflocation without invisibles, multiple selections allowed and showing package contents
set jpegfolderpath to (chosenfolder as text)
set lastjpegfolderpath to jpegfolderpath
return jpegfolderpath
on error errmsg number errnum
return missing value
end try
end askforjpegfolderpath
-- I indicate if a given item path exists
on itempathexists(itempath)
try
set itemalias to itempath as alias
return true
on error
return false
end try
end itempathexists
-- I return the text items of a text separated by the given delimiter
on gettxtitems(delim, txt)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delim}
set txtitems to text items of txt
set AppleScript's text item delimiters to oldDelims
return txtitems
end gettxtitems
-- I display info messages
on dspinfomsg(infomsg)
tell me
activate
display dialog infomsg buttons {"OK"} default button 1 with icon note with title mytitle
end tell
end dspinfomsg
-- I display error messages, hopefully rather seldom :)
on dsperrmsg(errmsg, errnum)
set msg to "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")"
tell me
activate
display dialog msg buttons {"OK"} default button 1 with icon stop with title mytitle
end tell
end dsperrmsg