Automatically print out PDF documents with Skim

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 :wink:


-- 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

@ Christiaan:

I imagine you’ve tried this – so please take it in the spirit of helpfulness – and it’s not an elegant solution (but maybe Applescriptable?), but have you tried combining the PDFs into one big PDF using Preview? It’s fairly simple and while it would take a few steps upfront, you could then set the Print Dialog once, hit Print and walk away.

You do it by selecting the pages in the sidebar from the 2nd document (CMD-A works) and dragging them to the end of the pages in the first document’s drawer – presto, ONE PDF with the pages from both PDFs. If you’re just doing this to print once, no need to hit save, but if you want to do it multiple times, you could do a Save As with it and have the combined PDF available for later use.

And if this process could be AppleScriptable, so much the better. Just a thought…

Alex

I needed a script similar to what Martin provided, but which exported PDF files that I’d annotated in Skim with the embedded notes. In other words, take PDFs I’d marked up in Skim and make them readable by other PDF viewers.

Here is what I came up with. It was horribly quick work, and the script is not user-friendly, in that it overwrites the original files (so be sure you are working with a copy of your originals when you drop files on this droplet).

In any case, here it is. Thank you, Martin, for providing the original script. I’m sorry to have mangled it… :confused:


-- This script batch exports dropped PDF documents from Skim
-- with embedded notes.
-- Very quickly hacked from an original script written by
-- Martin Michel.  Original script available at:
-- http://bbs.macscripter.net/viewtopic.php?id=25351
--
-- IMPORTANT: This script overwrites the original
-- files that are dropped!

-- Skim is available at <http://skim-app.sourceforge.net>.
-- The script requires Skim 1.1.3 (or higher).

property mytitle : "Batch export PDFs with Embedded Notes using 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 export them with Embedded Notes using Skim."
	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
		
		-- Export the dropped PDF files without
		-- user interaction
		repeat with pdffilepath in pdffilepaths
			tell application "Skim"
				open (pdffilepath as alias)
				save document 1 in pdffilepath as "PDF With Embedded Notes"
				close document 1
			end tell
		end repeat
	on error errmsg number errnum
		my dsperrmsg(errmsg, errnum)
	end try
end open

-- 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