Skim, a free, versatile and well scriptable PDF viewer for Mac OS X, now also supports to print out PDF documents without invoking the print dialog using AppleScript:
tell application "Skim"
open (POSIX file "/Users/martin/Desktop/sample.pdf")
print document 1 without print dialog
close document 1
end tell
Below you can find a sample script that will batch print dropped PDF documents using Skim. It will asked you to choose a printer to be used for the PDF batch printing and conveniently restore your old default printer after it finished the job.
To use the script on your fine Mac, please choose to ‘Open this Scriplet in your Editor’ and then save it as an Application bundle. The script requires Skim 1.1.3 (or higher) and was succesfully tested on Mac OS X 10.5.2.
But remember to save the trees and go paperless wherever possible
-- This script batch prints out dropped PDF documents without invoking
-- the print dialog using the free PDF viewer Skim, You will be asked to
-- choose a printer to be used for the PDF batch print out. The script
-- will also restore your previously set default printer after finishing
-- its job.
-- Skim is available at <http://skim-app.sourceforge.net>.
-- The script requires Skim 1.1.3 (or higher).
property mytitle : "Batch print out PDFs with Skim"
-- I am called when the user opens the script with a double-click
on run
set infomsg to "I am a hungry AppleScript droplet!" & return & return & "Drop a bunch of PDF documents onto my icon to batch print them using Skim and the currently set printer."
my dspinfomsg(infomsg)
end run
-- I am called when the user drops Finder items onto the script icon
on open droppeditems
try
set droppeditems to droppeditems as list
set pdffilepaths to {}
-- looking for dropped PDF files
repeat with droppeditem in droppeditems
set iteminfo to info for droppeditem
-- ignoring folders
if not folder of iteminfo then
-- file should end with ".pdf" suffix
if name of iteminfo ends with ".pdf" then
set pdffilepaths to pdffilepaths & (droppeditem as Unicode text)
end if
end if
end repeat
-- no PDF files were dropped onto the droplet
if pdffilepaths is {} then
set errmsg to "You did not drop any PDF documents onto me."
my dsperrmsg(errmsg, "--")
return
end if
-- get current default printer, so that we can
--later restore this value
set olddefprinter to my getdefprinter()
-- asking the user to choose a printer for the
-- PDF batch printing process
set newdefprinterset to my choosedefprinter()
-- user did not choose a printer and canceled
if newdefprinterset is false then
return
end if
-- if Skim was not running until now, we will politely
-- quit it after finishing our batch print out
set closeskim to false
if not my appisrunning("Skim") then
set closeskim to true
end if
-- batch printing the dropped PDF files without
-- user interaction
-- the currently set printer is used
repeat with pdffilepath in pdffilepaths
tell application "Skim"
open (pdffilepath as alias)
print document 1 without print dialog
close document 1
end tell
end repeat
if closeskim is true then
tell application "Skim"
quit
end tell
end if
-- restoring the previously used default printer
my setdefprinter(olddefprinter)
-- catching unexpected errors
on error errmsg number errnum
my dsperrmsg(errmsg, errnum)
end try
end open
-- I am asking the user to choose a default printer
-- to be used for the PDF batch printing
on choosedefprinter()
-- getting all printers and their names
tell application "Printer Setup Utility"
set allprinters to every printer
set printernames to name of every printer
end tell
-- creating numbered menu items
set counter to 0
set menuitems to []
repeat with printername in printernames
set counter to counter + 1
set menuitem to "[" & (counter as Unicode text) & "] " & printername
set menuitems to menuitems & menuitem
end repeat
-- asking the user to choose a new default printer
choose from list menuitems cancel button name "Quit" OK button name "Select" with title mytitle with prompt "Please choose a printer for PDF batch printing:" without multiple selections allowed and empty selection allowed
if result is false then
-- user hit the quit button...
return false
else
-- getting the leading number part from the chosen menu item
set usrchoice to result as list
set chosenmenuitem to item 1 of usrchoice
set spaceoffset to offset of " " in chosenmenuitem
set printerid to (characters 2 through (spaceoffset - 2) of chosenmenuitem) as integer
set chosenprinter to item printerid of allprinters
-- setting the new default printer
my setdefprinter(chosenprinter)
return true
end if
end choosedefprinter
-- I am returning the current default printer
on getdefprinter()
tell application "Printer Setup Utility"
return current printer
end tell
end getdefprinter
-- I am setting the default printer to the given printer(name)
on setdefprinter(defprinter)
tell application "Printer Setup Utility"
set current printer to defprinter
end tell
end setdefprinter
-- I am indicating if a given application is currently running or not
-- only the (full) application name must be given, e.g. "Address Book"
on appisrunning(appname)
tell application "System Events"
set processnames to name of every process
end tell
if appname is in processnames then
return true
else
return false
end if
end appisrunning
-- 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
on dsperrmsg(errmsg, errnum)
tell me
activate
display dialog "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
end tell
end dsperrmsg