AppleScript to convert HTML to PDF via Smile

This has been allways a tricky thing (convert a web page to PDF, preserving the more info possible, including text and clickable links), but since Smile 3.2 is a one-liner I thought I should share (specially when I didn’t find examples for the pageloaded event). This will save a “random” web page to your desktop, in a file called “file.pdf”. If you wanna see it in action, just remove “visible:false”:

script callback
	on pageloaded w
		save w in "~/Desktop/file.pdf"
		beep 2
		close w
	end pageloaded
end script

set webPage to "http://www.qilania.com/"

make new web window with properties ¬
	{path name:webPage, script:callback, visible:false}

It won’t preserve properly the formatting of certain web pages or embedded plug-ins contents, so if you need the screenshot for aesthetic purposes, Smile also provides a command called take screenshot, which will make a image of the rendered page (use it instead of “save w” as “take screenshot of w in …”). You may first resize the window to your favorite size, as the screenshot will only contain the visible area of the web window, but that is left as an exercice for the reader 😉

Very useful code for many tasks (web related), specially if Smile is your main script editor.

AppleScript to count pages of PDF documents via Skim

I found Skim to be the perfect replacement for Preview (ie, a fast app to review PDF documents with notes, etc.). And it also provides a decent AppleScript dictionary, so it’s also the perfect app to make one of those great time-savers. Follows sample code you can save as a droplet (and process a bunch of PDF files via drag & drop) or as a compiled script to be run from any script launcher:

open (get selection of application "Finder")

on open pdflst
	set cnt to 0
	repeat with i from 1 to count pdflst
		--> just in case comes from run handler...
		tell application "Finder" to set pdfFile to (pdflst's item i as alias)
		tell application "Skim"
			open pdfFile
			set cnt to cnt + (page count of (get info of document 1))
			close document 1 saving no
		end tell
	end repeat
	
	display dialog ("" & cnt & " pages for " & (count pdflst) & " documents...") with icon note
end open

I know it’s trivial code, but still you must spend some time to find a solution for your PDF-page-count needs, and this is a fast and free one.