Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to print a 'daily' URL via Firefox' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to print a 'daily' URL via Firefox
Authored by: cfoster on Jan 30, '06 03:26:59AM

Here's another option that doesn't require any browser, or require that you allow access to the user interface via AppleScript (which can be a security concern). It uses the 'curl' and 'grep' commands to find just the cartoon in the web page. It then saves the image to a temp file, and uses Preview to print it.

-- URL is in the form of {URL_BASE, "yyyymmdd.html"
set UrlBase to "http://dilbert.com/comics/dilbert/archive/dilbert-"
set DayString to day of (current date)
set MonthString to month of (current date) as number
set YearString to year of (current date)

--Add zero padding for days/months less than 10 (required URL format)
if (DayString < 10) then set DayString to "0" & DayString
if (MonthString < 10) then set MonthString to "0" & MonthString

-- Set date portion of URL
set dateString to YearString & MonthString & DayString as string

-- Retrieve the HTML block that contains today's Dilbert
set img_html_block to do shell script "curl http://dilbert.com/comics/dilbert/archive/dilbert-" & 
	dateString & ".html | grep -e SRC=\\"/comics/dilbert/archive"

-- Find the specific location of the img src string and truncate to its start.
set img_path_start to offset of "/comics/dilbert/archive" in img_html_block
set img_html_block to text img_path_start through (length of img_html_block) of img_html_block

-- Find where the path ends and build the absolute path of the comic. Determine gif or jpg file type.
set img_ext to ".gif"
set img_path_end to offset of img_ext in img_html_block
if (img_path_end is equal to 0) then
	set img_ext to ".jpg"
	set img_path_end to offset of img_ext in img_html_block
end if
set comic_path to "http://dilbert.com" & (text 1 through (img_path_end + 3) of img_html_block)

-- Grab just today's comic and save it to a temp file.
set temp_file to "/private/tmp/dilbert" & dateString & img_ext
do shell script "curl " & comic_path & " >" & temp_file

-- Check if Preview is already running.
tell application "Finder"
	try -- If this fails, preview wasn't running.
		set preview_was_running to ((name of process "Preview") is equal to "Preview")
	on error
		set preview_was_running to false
	end try
end tell

-- Launch Preview (if necessary) and have it print the image.
tell application "Preview" to print ((POSIX file temp_file) as text)

-- We should quit preview if it wasn't running before this script ran.
if (preview_was_running is equal to false) then
	delay 1 -- give it a second to fire off the image to the print queue.
	tell application "Preview" to quit
end if

-- Clean up our temp file.
do shell script "rm " & temp_file



[ Reply to This | # ]