Save to Instapaper from NetNewsWire

February 9th, 2009  |  Published in ruby  |  2 Comments

At some point some applescripts I found for posting from NetNewsWire to Instapaper broke. A few days ago, during one of my rare jaunts to the bottom of the Instapaper homepage I came across a reference to the Read Later API. I wrote it down on my todo list as a thing to deal with over lunch some day.

The API involves nothing more than an HTTP request with a half-dozen parameters, a few of which are optional. I stopped shy of simulating the wonderful Instapaper response popup, settling instead on a system beep:


   #!/usr/bin/env ruby

   require 'rubygems'

   require 'open-uri'

   require 'openssl'

   require 'appscript'

   include Appscript

   require 'osax'

   include OSAX



   username = "USERNAME"

   password = "PASSWORD"

   nnw = app("NetNewsWire")



   OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE



   item = nnw.selectedHeadline.get

   url = URI.encode(item.URL.get).gsub!(/^htt.+?\/\//, "")

   title = item.title.get

   description = item.description.get.gsub!(/<.+?>/, "") 



   selection = description.length > 150 ? description.slice(0,150) + " ..." : description



   base_url = "https://www.instapaper.com/api/add?"

   params = "title=#{title}&url=#{url}&username=#{username}&password=#{password}&selection=#{selection}"

   post_url = "#{base_url}#{params}"





   begin

     response = open(URI.encode(post_url))

     osax.beep

     # or display a dialog:

     # osax.display_dialog("Posted.")

     rescue => ex

       case ex.message.gsub(/\s.*/, "")

         when "400" : message = "Error: Bad Request."

         when "403" : message = "Bad password/username."

         when "500" : message = "Server error."

       end

       osax.display_dialog(message)

   end

Responses

  1. Post NetNewsWire Items to Evernote :: dot unplanned says:

    February 17th, 2009 at 8:23 am (#)

    […] scripting syntax is as clean as anything, so it was pretty simple to adapt my NetNewsWire => InstaPaper script to save stuff from NNW to […]

  2. … or, you know, just keep the power tools in the garage where I can find them :: dot unplanned says:

    May 30th, 2012 at 9:03 pm (#)

    […] immediately available from a menu (e.g. sending a news item to Instapaper), were easily available with a simple script. And NNW also supports Automator, which takes a lot of the programming out of problems you might […]

Leave a Response

© Michael Hall, licensed under a Creative Commons Attribution-ShareAlike 3.0 United States license.