set filepath to (((path to desktop) as string) & "test.pdf")
tell application "Preview"
activate
open file filepath
print file filepath
close document filepath
end tell
If preview is closed when the script is executed, it prints fine, but it doesn’t close the file, and eventually results in the error “AppleEvent timed out”.
If preview is open when the script is executed (but no windows are open), the file will open but it will not even print. Same time out error eventually.
Nothing happened with that script - including no errors.
So I’ve moved on to GUI scripting and came up with this. It works. Is this my best option? Any way to make it better? The delays are a bit annoying…
set filepath to (((path to desktop) as string) & "test.pdf")
tell application "Preview"
activate
open file filepath
end tell
tell application "System Events"
tell process "Preview"
delay 1
keystroke "p" using command down
delay 1
keystroke return
delay 1
keystroke "w" using command down
end tell
end tell
Save this to the desktop as an application bundle.
on open _files
repeat with f in _files
set pf to quoted form of POSIX path of (f as text)
do shell script "lpr " & pf
end repeat
end open
then run this script :
tell application "Finder"
open "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources"
end tell
Open the file “PrintMonitorFolderIcon.icns”, in Preview, select item 3 from the sidebar, and copy it to clipboard.
select the printer.app in Finder, show info for it, select the icon in the upper left corner and paste the icon on to it!
From now onwards, you can just drag and drop files onto it, and you can also put it onto Finders toolbar, (if you hold down cmd, while you click on the libelle in the upper left corner, you can change view options of the toolbar icons, do try it.
Something in Preview that you actually can script!
I made a droplet using the Preview snippet supplied by mouramartins.
You can use that as a replacement for the print droplet in the post above.
on open _files
repeat with f in _files
tell application "Preview"
print (f as text)
end tell
end repeat
end open
Edit I just want to add, that I believe the Preview version to work for me, since I have been inside the package of Preview.app (the bundle), there I have been in the folder Contents/Resources, and edited the info.plist file, to state that it is scriptable. I have read that for this to work on Os X Mountain Lion, you have to make a copy of Preview.app, and do the hack on the copy, due to code signing. Or Preview won’t work.
But I don’t know, maybe the print command always works in Preview.
This is part of a folder actions script I’ve been working on. The print part works great.
on adding folder items to thisFolder after receiving addedItems
tell application "Finder"
set finishedPrint to folder "a file path"
end tell
repeat with anItem in addedItems
...other stuff...
tell application "Finder"
print anItem
delay 10
move anItem to finishedPrint with replacing
end tell
end repeat
end adding folder items to
I don’t think I really need the delay 10 in there, but it doesn’t slow anything critical down for me. It gives Preview time to open the doc, send it to the printer and close it down before trying to process the next file. I should note though that I had to change the default program for PDFs back to Preview. Illustrator would pop up import dialogs that interrupted the script. Never bothered with trying Acrobat, since Preview takes up less RAM sitting open all day. (I use Illustrator for 95% of my work, hence changing the default.)