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

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.