Saturday, September 9

Downloading the New York Times Crossword with Automator

Wordplay got me into doing crosswords. If Jon Stewart, Bill Clinton, and the Indigo Girls are all into something (not to mention Ken Burns) I’d be crazy not to be all up on that. I bought the yearly online subscription to the New York Times puzzle and have been doing (attempting) it pretty much every day.

I made a workflow with Apple’s Automator to download each day’s puzzle from the Times’ website and print it out. If you’re in a similar position as I (you have the subscription, Across Lite, and Mac OS X 10.4) then give it a try: Print Crossword.workflow.

The overall workflow is pretty straightforward:
  1. Start with the New York Times crossword page URL
  2. Download the page, with proper authentication
  3. Find the URL to today’s puzzle
  4. Download today’s puzzle
  5. Open today’s puzzle
  6. Print today’s puzzle
There were two tricky bits at #3 and #6, which I had to solve using shell scripting and AppleScript, respectively.

While Safari scripting does a fair amount (I was happy it took care of using my NYTimes.com cookie for the authentication), it doesn’t handle #3, which actually surprised me. The task I needed is to get the URL linked to with the words “Today’s Puzzle.” Here’s the script I used:
grep -o "<a href=\"[^\"]*\">Today's Puzzle" $1 | sed -n -e "s|<a href=\"\\([^\"]*\\)\".*|http://select.nytimes.com/premium/xword/\\1|p"
rm -rf $1
Yeah, it’s a bit of a hack but it works. (If anyone has a better line, please leave it in the comments.) The grep is there to cut down on the data going in to sed, since sed will print out the unmatched start of the line (and matching the start with a regexp is too slow).

I was impressed with how simple it was to include a shell script in the workflow, given the typing going on. But “Files/Folders” became a path, and the stdout text became “URLs” for the next step. Neat!

Getting Across Lite to print the puzzle was the second tough bit. Across Lite is a reasonable program, but, as is often the case with ported software has no AppleScript support, so I had to fall back on GUI Scripting instead of the standard “print” command. Here’s the code:

on run {input, parameters}
set xfile to item 1 of input

tell application "Finder"
open xfile
end tell

tell application "Across Lite v2.0"
activate
end tell

tell application "System Events"
tell process "Across Lite v2.0"
click menu item "Print" of menu "File" of menu bar 1
keystroke return
keystroke return
keystroke return
end tell
end tell

tell application "Across Lite v2.0"
delay 10
quit
end tell

tell application "Finder"
delete xfile
end tell

return input
end run
Not too much to say here. Three returns to get through the print choices, Page Setup, and Print dialog boxes, and a ten second delay to prevent a crash.

And there you go! I saved this as an iCal plug-in and scheduled it to go off every day at 7AM. Though, to be truthful, I could probably turn it off for Fridays and Saturdays… But now every day when I wake up I have a crossword puzzle to take with me to do on the shuttle.