Printing selected pages in Preview (Lion)

Since I have been on Lion something has been irritating me: I lost the keyboard shortcut cmd-alt P that existed in Snow Leopard to print directly the selected pages in Preview :frowning: . This morning I decided to tackle the subject as every saturday morning I have to print some pages from a long pdf file.
After some trial and errors, I managed it and would like to share it by posting it in case someone needs it.

“Before writing code that makes use of UI Scripting in Mac OS X, you will first need to manually configure the machine on which the script will run, so that it can respond to UI Scripting commands. To enable UI Scripting, launch the System Preferences application, and click on the Universal Access button. Next, enable the checkbox labeled Enable access for assistive devices” (http://www.mactech.com/articles/mactech/Vol.21/21.06/UserInterfaceScripting/index.html)

Type the following script in Applescript Editor.

tell application "System Events"
	tell process "Preview"
		set frontmost to true
		click menu item "Print." of menu "File" of menu bar 1
		repeat until sheet 1 of window 1 exists
		end repeat
		
		tell sheet 1 of window 1
			tell radio group 4
				tell radio button 3
					click
				end tell
				delay 0.5
			end tell
			click button "Print"
		end tell
	end tell
end tell

Save it in /Users/YourHouse/Library/Scripts that you can open via the Applescript menu in the top menu bar.

I use FastScript http://www.red-sweater.com/fastscripts/ to associate a cmd-alt-P to the script et voilà ! it works.:slight_smile:
(If you are french like me you have to replace “Print” by “Imprimer” and “File” by “Fichier” in the script.°

Model: iMac 21.5-inch, Mid 2011
AppleScript: 2.2.1
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Hello

With some simple changes, your script would be usable worldwide :wink:


tell application "System Events" to tell application process "Preview"
	set frontmost to true
	click menu item -1 of menu 3 of menu bar 1
	repeat until sheet 1 of window 1 exists
	end repeat
	
	tell sheet 1 of window 1
		tell radio group 4
			tell radio button 3
				value
				click
				repeat
					delay 0.1
					if value = 1 then exit repeat
				end repeat
			end tell -- radio button
		end tell -- radio group
		click button 1
	end tell -- sheet
end tell -- System Events

Yvan KOENIG (VALLAURIS, France) dimanche 29 janvier 2012 18:27:46