Print HTML file

I have a certain script-generated email that arrives with a single HTML file attached. I am trying to get the computer to print this file for me. I am not sure I am considering the problem properly or if there are better options, so I’m here asking for advice. Here is my current code:

-- bastardized from...
-- Paul Berkowitz's Save Attachments 2004

tell application "Microsoft Entourage"
	set attachFolderPath to (((path to MUD) as Unicode text) & "Saved Attachments:")
	set theMsg to item 1 of (get current messages)
	set msgAttachments to attachments of theMsg
	
	repeat with i from (count msgAttachments) to 1 by -1
		set theAttachment to item i of msgAttachments
		set attachName to name of theAttachment
		set fileSpec to (attachFolderPath & attachName)
	end repeat
end tell

tell application "Safari"
	open fileSpec
	delay 1 --give Safari time to load graphics
	print document 1 without print dialog
	close document 1
end tell

I don’t particularly like the delay between open and print, but if I don’t have it the page can print without graphics having loaded. The delay also gives the user (potentially me) time to interfere with Safari and the script, and if things are particularly slow the graphics may not fully load before printing happens.

I also don’t really like Safari popping up the new window and then closing it, it is distracting to the user should they be using the computer when this script fires off.

Is there an option other than Safari I could use? Is there a way to print the HTML file without anything opening and closing on-screen? Anything else I’m not considering?

Thank you.

Hi,

On Apple’s AppleScript site there is a subroutine
to wait until the page has loadad.
You can use it like this:

-- bastardized from...
-- Paul Berkowitz's Save Attachments 2004

tell application "Microsoft Entourage"
	set attachFolderPath to (((path to MUD) as Unicode text) & "Saved Attachments:")
	set theMsg to item 1 of (get current messages)
	set msgAttachments to attachments of theMsg
	
	repeat with i from (count msgAttachments) to 1 by -1
		set theAttachment to item i of msgAttachments
		set attachName to name of theAttachment
		set fileSpec to (attachFolderPath & attachName)
	end repeat
end tell

tell application "Safari"
	open fileSpec
	if page_loaded(20) is false then
		display dialog "The document " & attachName & " could not be loaded" buttons {"OK"} default button 1 giving up after 3
	else
		print document 1 without print dialog
		close document 1
	end if
end tell

on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded 

Thank you for the reply. Unfortunately I get an error when I try this code, so I will try my original code for a while to see how it works.

oh, I see.

Sorry, I forgot the little word my in this line:

if my page_loaded(20) is false then

because the handler is called within a tell block