Friday, February 01, 2008

Post to instapaper (AppleScript)

Update (26-Jun-09): This script doesn't handle more recent versions of InstaPaper very nicely. Another NNW-to-InstaPaper script, "NNWInstaPost", has appeared in the meantime and I suggest that you give that one a try ...


Here's a little script to post the currently selected headline in NetNewsWire (my favourite Mac OS X newsfeed reader) to your instapaper account.

You need to create an instapaper account before using this the first time. The script is not very good about errors, so just correct the issue and retry the script. It creates a short summary by "un-HTML'ing" the description, but it does this naively (using a long sed invocation), so it's restricted to a subset of HTML entities -- basically common Western ones.

To install this, select and copy the script from the box, paste it into the AppleScript Script Editor and drop it into NNW's script folder.

(*
* Submit NetNewsWire headines to instapaper for later reading. See http://www.instapaper.com/
* Create an instapaper account first, select a news item in NNW, then run this script.
* Assumes you're using Safari. Doesn't handle all html entities; works best on English feeds.
* Doesn't check if the submission succeeded, so you need to keep your eyes open. :-)
* -- Bruce Walker <bruce.walker@gmail.com> -- Feb 1, 2008
*)

on unhtml(html)
-- remove markup, convert common entities to ascii (the rest to "?"), nuke blank lines & restrict length
return do shell script "echo " & quoted form of html & "| sed -e 's/<[^>]*>//g' -e 's/&nbsp;/ /g' -e 's/&mdash;/--/g' -e 's/&lsquo;/`/g' -e 's/&ldquo;/``/g' -e 's/&#8220;/``/g' -e \"s/&rdquo;/''/g\" -e \"s/&#8221;/''/g\" -e \"s/&#8217;/'/g\" -e \"s/&rsquo;/'/g\" -e \"s/&#39;/'/g\" -e 's/&[^;][^;][^;]*;/?/g' -e \"s/'/\\\\\\'/g\" | tr '\\r\\n' ' ' | dd ibs=1 count=192"
end unhtml

tell application "NetNewsWire"
set NNWURL to URL of selectedHeadline
set NNWTitle to my unhtml(get title of selectedHeadline)
set NNWSummary to my unhtml(get description of selectedHeadline) & " …"
end tell

tell application "Safari"
-- submit to instapaper using Safari's saved session/account cookies
set foo to do JavaScript "window.open('http://www.instapaper.com/b?v=3&u='+encodeURIComponent('" & NNWURL & "')+'&t='+encodeURIComponent('" & NNWTitle & "')+'&s='+encodeURIComponent('" & NNWSummary & "'),'t','toolbar=0,resizable=0,status=1,width=250,height=150')" in document 1
end tell

Enjoy!
Post a Comment