Preview App Quick-Print Dialog

The following script prints one or more pages of the document displayed in the Preview app. Print options are found in the lp man page and here.

-- revised 2024.02.03

try
	tell application "Preview" to set thePath to path of document 1
on error
	tell application "Preview" to quit
	display dialog "Preview app or file was not found" buttons {"OK"} cancel button 1 default button 1 with icon stop
end try

set thePages to text returned of (display dialog "Enter space-separated pages and page ranges to print" default answer "")
set text item delimiters to {"  ", " "}
set thePages to text items of thePages
set text item delimiters to {","}
set thePages to thePages as text
set text item delimiters to {""}

try
	do shell script "lp -o fit-to-page -P " & quoted form of thePages & " " & quoted form of thePath
on error errorMessage
	display dialog "A print error occurred" buttons {"View Error", "OK"} cancel button 2 default button 2 with icon stop
	display dialog errorMessage buttons {"OK"} default button 1
end try