Printing a document open in Preview (Lion)

I am trying to add a script to print the document that is currently open in Preview. I have tried several different approaches, and can’t egt them to work.

Example 1: I have tried both P and “P” - neither work. With just P, i get an error, and with “P” the print window doesn’t open.

tell application "System Events"
	tell process "Preview"
		set frontmost to true
		keystroke "P" using command down
		repeat until sheet "Print" of window 1 exists
		end repeat
		keystroke return
	end tell
end tell

Then I tried:

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 "Print" of window 1 exists
		end repeat
		keystroke return
	end tell
end tell

which returns the error “System Events got an error: Can’t get menu item “Print…” of menu “File” of menu bar 1 of process “Preview”.”

I would appreciate any help in fixing this…

Cheers,
Allan

Hi,

script #1
keystroke “P” using command down is the same as keystroke “p” using {command down, shift down}
so use

 keystroke "p" using command down

script #2
… are not 3 dots, it’s a single character called horizontal ellipsis (on US Keyboard ⌥; )


activate application "Preview"
tell application "System Events"
	tell process "Preview"
		click menu item "Print." of menu "File" of menu bar 1
		repeat until sheet "Print" of window 1 exists
		end repeat
		keystroke return
	end tell
end tell

Hi Stefan,

Thanks for the quick reply - the ellipsis solved the error, but the script runs, waiting for the print window to “exist” even after the print pop-up is up and waiting for the “return.” I removed the repeat statement, and it works.

Thanks again!
Cheers,
Allan

There is no sheet “Print” so the script waits forever.
sheet 1 should work

It would be easy to edit the posted script so that it may be used worldwide .


activate application "Preview"
tell application "System Events"
	tell process "Preview"
		click menu item -1 of menu 3 of menu bar 1
		repeat until sheet 1 of window 1 exists
		end repeat
		keystroke return
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) lundi 14 janvier 2011 20:35:12