Changing this piece of code to ask only for Height and let Width Fall?

Hello,
I have an old script that opens up a bunch of pdf files, with multiple pages that allows me to create jpegs of each one.

The current issue is, it asks for a specific width and heigh for the jpeg…

I would like to have the height be 72px but the width to fall so it stays in proportion.

Here is the part of the script that needs to be modified and below, what I attempted to do, but it doesn’t work ;(

thanks
babs


--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}
	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


This is what I tried to do, but it didn’t work ;(

--I ask the user to provide the width and height to be used for the JPG images
on askforheight()
	-- 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 height to be used for the JPG conversion:" & return & "(Example: \"800\", 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}
	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 askforheight()
		else
			set enteredheight to (item 2 of usrinputparts) as text
			try
				set height to enteredheight as integer
				set lastwidthandheight to (height) as text
				return {height}
			on error
				my askforheight()
			end try
		end if
	end if
end askforheight


I have a thought…I think this can be done???

Is it possible to call an action in Photoshop from an applescript? For some reason I think that is doable, but not 100% sure… and if that is the case, I could always run this script in its entirety…(I will post the entire script here)…and then maybe have it go back to the folder of jpegs and resize them all in photoshop to be 1 inch in hight and let the width fall…cause I know I can do that in a photoshop action…

Of course I can set up a droplet as a separate entity if I have to, but if it can be done in one shot…it’s worth a try :wink:

Just a thought…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 --resampleHeightWidth " & height & " " & width & " " & 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

When I have to work with PDFs, I use a free application named Skim.app which is AppleScript aware.

Here is a short piece of code returning the dimensions of a page of the front most PDF open in Skim.


tell application "Skim"
	tell document 1
		set {{xCleft, yCbottom, xCright, yCtop}, {xLeft, yBottom, xRight, yTop}} to {content bounds, bounds} of page 3
		--> {59, 779, 536, 72}
		--> {0, 842, 595, 0}
	end tell
end tell
set cWidth to xCright - xCleft
log cWidth
--> 477
set cHeight to yCbottom - yCtop
log cHeight
--> 707
set pWidth to xRight - xLeft
log pWidth
--> 595
set pHeight to yBottom - yTop
log pHeight
--> 842

# controls
cWidth + (2 * xCleft) = pWidth
--> true
cHeight + (2 * yCtop) = pHeight
--> true

I know, the way parameters are organiozed is a bit surprising at first look but it’s a Skim feature.
Knowing the page dimensions (full page dimensions or contents ones), it’s easy to calculate the dimensions of the jpeg to create keeping the proportions.

Here is a larger script showing a way to save separate pages using only free tools.


tell application "Skim"
	tell document 1
		set {{xCleft, yCbottom, xCright, yCtop}, {xLeft, yBottom, xRight, yTop}} to {content bounds, bounds} of page 3
		--> {59, 779, 536, 72}
		--> {0, 842, 595, 0}
	end tell
end tell
set cWidth to xCright - xCleft
log cWidth
--> 477
set cHeight to yCbottom - yCtop
log cHeight
--> 707
set pWidth to xRight - xLeft
log pWidth
--> 595
set pHeight to yBottom - yTop
log pHeight
--> 842

# controls
cWidth + (2 * xCleft) = pWidth
--> true
cHeight + (2 * yCtop) = pHeight
--> true

tell application "Skim" to tell document 1
	# grab the contents of page 3 in TIFF format
	grab page 3 for {xCleft, yCbottom, xCright, yCtop} as "TIFF"
	# If you want you may grab the conternts in format PDF using :
	# grab page 3 for {xCleft, yCbottom, xCright, yCtop} as PDF # no quotes
	set the clipboard to result
end tell
tell application "TextEdit"
	activate
	make new document
end tell
tell application "System Events" to tell process "TextEdit"
	keystroke "v" using {command down}
end tell
tell application "TextEdit"
	save document 1 in file ((path to desktop as text) & "wasInPdf.rtfd")
end tell

# Now, the contents is available in the file :
# (path to desktop as text) & "wasInPdf.rtfd:Pasted Graphic.tiff"
# You may manipulate it with Image Events

# If you choose the format PDF, you will get the file :
# (path to desktop as text) & "wasInPdf.rtfd:Pasted Graphic.pdf"

KOENIG Yvan (VALLAURIS, France) jeudi 1 août 2013 11:03:58

In theory, you only need to delete (or comment out) the askforwidthandheight() handler and the call to it and change the “sips” code line from .

set command to "/usr/bin/sips --resampleHeightWidth " & height & " " & width & " " & quoted form of outputline

. to .

set command to "/usr/bin/sips --resampleHeight 72 " & quoted form of outputline

But one thing to be aware of with “sips” ” as with Image Events, which is based on it ” is that “height” and “width” actually mean “shorter axis” and “longer axis”. If an image is in portrait mode, its “height” and “width” are juxtaposed.

Hi Yvan and Nigel,

Thank you both for this info!!
I will go back to my students office and see how it goes…Lots to play with and study here!!
Will keep you both posted!!!

Cheers,
babs

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


Ah. I see. The asked-for width is used in the pdf2jpg() handler too, I think. I’m looking at it now to see if it can be ditched.

Thanks Nigel!!! Much appreciated!!
babs :slight_smile:

OK. I don’t know if this will be useful to you or not because Martin’s script expects to find a command-line tool he wrote himself in the script bundle’s Resources. This tool is what converts the PDFs to JPEGs. There’s probably a download link and installation instructions in the post where you found the script.

My own contribution here has been to add a couple of properties which, if set as below, make the script use the fixed-“height” code. If ‘usingFixedHeight’ is set to ‘false’, the script should ask for the dimensions as originally.


-- 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 adaptation by Nigel Garvey allows a fixed "height" and a proportional "width" without the need to ask the user for image dimensions.
-- 01.08.2013
-- Couldn't be fully tested without Martin's command-line tool.

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

-- NEW: If 'usingFixedHeight' is true, use the 'fixedHeight' value and size the image proportionally instead of asking for the dimensions. To "sips", "height" means "the lesser dimension".
property usingFixedHeight : true
property fixedHeight : 72

-- 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
		if (usingFixedHeight) then
			-- NEW: use these values if using a fixed height.
			set {width, height} to {missing value, fixedHeight}
		else
			-- Otherwise, ask for the dimensions
			set {width, height} to my askforwidthandheight()
			if width is missing value and height is missing value then
				return
			end if
		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
			if (usingFixedHeight) then
				-- NEW: this command line for a fixed height and proportional width.
				set command to "/usr/bin/sips --resampleHeight " & height & " " & quoted form of outputline
			else
				-- Otherwise this one for the dimensions entered by the user.
				set command to "/usr/bin/sips --resampleHeightWidth " & height & " " & width & " " & quoted form of outputline
			end if
			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

Hello.

I found it timely to post this link from my browser history for sharpening the quality of thumbnails, by using Image Magick.

It’s OK. If you downloaded the script from the 10.5 and higher link Martin gives in this post, then it already has the tool in its Resources folder. All you have to do is replace the script text with that in post #9 above ” which works! :slight_smile:

Hi Nigel,

Ok, I have a good working script here thus far, (thanks to your link to the older one), that will at least ask the resolution and grab each page and save a jpg, and it places it in the same folder.

Before I get to the resize issue (height and width), there is one problem with the script…The resolution part is not working correctly? I was just doing 72 DPI the default and it worked fine, I just tried making the jpegs 300 DPI but it still makes a 72 DPI one? :frowning:

Before I work on tackling the resize part, can you see why the resolution is 72 dpi, no matter what value I type in the dialog box?

Here is the script I am now starting with… I can’t find anything that would fix this, and my guess is it takes place in the system, where maybe it is my operating system that is failing it. I am on 10.8.3. If that helps!!!

thanks!!
babs

-- author: Martin Michel
-- eMail: martin.michel@macscripter.net
-- created: 01.04.2008
-- version: 0.1
-- tested on:
-- ¢ Mac OS X 10.5.2
-- ¢ Intel & PowerPC based Macs

-- 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. Until now, all pages of a PDF file
-- are converted to JPG. It would be a nice feature,
-- if the user could also choose only certain page
-- numbers to be converted. Future?

property mytitle : "PDF2JPG"
property batchresolution : 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 JPG images."
	my dspinfomsg(infomsg)
	return
end run

-- I am the main function controlling the script flow
on main(droppeditems)
	try
		-- initializing important script properties
		set batchresolution to missing value
		-- 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
		-- processing the PDF files
		repeat with pdfpath in pdfpaths
			-- getting the image resolution to be used fot the PDF2JPG conversion
			if batchresolution is missing value then
				set resolution to my askforresolution(pdfpath)
			else
				set resolution to batchresolution
			end if
			-- did the user provide a resolution?
			if resolution is not missing value then
				-- yes, so let's convert the PDF to JPG
				my pdf2jpg(pdfpath, resolution)
			end if
		end repeat
		-- catching unexpected errors
	on error errmsg number errnum
		my dsperrmsg(errmsg, errnum)
	end try
end main

-- I am searching 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 as Unicode text))
		end if
	end repeat
	return pdfpaths
end getpdfpaths

-- I am returning the Posix path to the Python script
-- responsible for the PDF manipulation, which is
-- located in the application bundle
on getpyscriptpath()
	set pyscriptpath to ((path to me) as Unicode text) & "Contents:Resources:pdflib.py"
	return (POSIX path of pyscriptpath)
end getpyscriptpath

-- I am returning the total page count of a given PDF file
-- [PDF file path must be passed as an unquoted Posix path]
on getpagecount(pdfpath)
	set action to "getpagecount"
	set cmd to "python" & space & quoted form of (my getpyscriptpath()) & space & action & space & quoted form of pdfpath
	set cmd to cmd as «class utf8»
	set pagecount to (do shell script cmd) as integer
	return pagecount
end getpagecount

-- I am converting a given PDF file to JPG
-- [PDF file path must be passed as an unquoted Posix path]
on pdf2jpg(pdfpath, resolution)
	set action to "pdf2jpg"
	set cmd to "python" & space & quoted form of (my getpyscriptpath()) & space & action & space & quoted form of pdfpath & space & resolution
	set cmd to cmd as «class utf8»
	do shell script cmd
end pdf2jpg

-- I am asking the user to provide a value for the resolution
on askforresolution(pdfpath)
	set msg to "Please enter a resolution used for the JPG conversion of the followng PDF file (72-600):"
	try
		tell me
			display dialog msg default answer "72" buttons {"Use value for batch", "Cancel", "Enter"} default button 3 with title mytitle
			set dlgresult to result
		end tell
	on error errmsg number errnum
		-- user hit 'Cancel' button :(
		if errnum is equal to -128 then
			return missing value
		end if
	end try
	set resolution to text returned of dlgresult
	-- empty input...asking again :)
	if resolution is "" then
		my askforresolution(pdfpath)
	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(pdfpath)
		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(pdfpath)
		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(pdfpath)
		else
			-- finally...
			if button returned of dlgresult is "Use value for batch" then
				set batchresolution to resolution
			end if
			return resolution
		end if
	end if
end askforresolution

-- I am displaying 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 am displaying 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

The script to which I linked looks like the one in post #2 above. Start with that. Start with a copy in its just-unzipped state, open it in AppleScript Editor, replace its entire text with the code in post #9, compile and save, and try it.

If the resolution figure’s ignored, I don’t know what to suggest, since it’s handled by Martin’s command-line tool and I don’t know anything about that. The relevant line appears to be the one at the top of the pdf2jpg() handler:

set command to quoted form of (my getcltoolpath()) & " -pdf " & quoted form of pdfpath & " -out " & quoted form of jpegfolderpath & " -dpi " & resolution

The resolution figure appears to be added to the command OK.

Hi Nigel,

Well, after lots of trial and error) I just couldn’t get the 300dpi part to work, so I will use the action wizard in acrobat to at least open up each page and save as a tiff (300 DPI), then I can create a droplet in PS to create the 300 and 72 DPI jpegs.
It’s one extra step, but at least I am comfortable using those products, and since I was just trying to help out someone, seems like the best I can do at this point. I do thank you and Yvan for all your help!!!

Thanks!!!
babs