Posts Tagged ‘RSS’

Getting RSS from Apple mail to NetNewsWire

29 Comments »

I have been using Apple Mail to monitor my RSS feeds.  I like it because it’s right there when I check email.  Unfortunately, while I can set up my mail account using IMAP and have everything in sync regardless of whether I use the Mac Pro in the study, my Macbook Air, or my iPhone, the RSS feeds do not sync up.  So, sitting here in the hospital, I am unable to keep up with all the feeds on my main machine.  I did a bit of searching and found that NetNewsWire will do the trick of keeping them all synchronized (including the iPhone).

Downloaded…Installed…now, just import my RSS feeds from Apple Mail…hmmmmm…. search Google… only hit is this blog entry bemoaning the lack of this capability.

Well now, a bored programmer, laid up in the hospital, feeling just fine, and sharing the same need can’t just let a situation like this stand.

So, here’s a ruby script that will read the RSS feeds from your Apple Mail and create an opml file named AppleMailRSS.opml on your desktop.  This file will import into NetNewsWire and, though I’ve not tested anything else, presumably any other RSS reader that imports opml files.

(Update note: (March 21, 2009) I’ve updated the script to handle any RSS feeds in folders in Apple Mail. Thanks Sebastion for pointing this out. I was not using folders in Apple Mail so I missed this detail.)

require 'rexml/document'

skeleton = <<EOSTRING
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.1">
  <head>
    <title>Subscriptions from Apple Mail</title>
    </head>
  <body>
  </body>
</opml>
EOSTRING

def getFolder(node,folders)
  return node if folders.length == 0
  folder = folders.shift
  node.each_element("outline[@title='#{folder}']") do |fNode|
    return getFolder(fNode,folders)
  end
  fNode = REXML::Element.new 'outline'
  fNode.attributes['text'] = folder
  fNode.attributes['title'] = folder
  node << fNode
  return getFolder(fNode,folders)
end

def addRSSNode(body,folders,title,url)
  ol = REXML::Element.new 'outline'
  ol.attributes['text'] = title
  ol.attributes['description'] = ''
  ol.attributes['title'] = title
  ol.attributes['type'] = 'rss'
  ol.attributes['version'] = 'RSS'
  ol.attributes['htmlUrl'] = ''
  ol.attributes['xmlUrl'] = url
  getFolder(body,folders) << ol
end

doc = REXML::Document.new(skeleton)
body = REXML::XPath.match(doc,'//body')[0]
Dir.chdir()
Dir.chdir('Library/Mail/RSS')
Dir.glob('**/*.rssmbox') do |mbox|
  m = mbox.match(/(.*\/)*(.*).rssmbox/)
  folders = m[1] ? m[1].split('/') : []
  title = m[2]
  File.open("#{mbox}/Info.plist") do |file|
    url = file.read.match(/RSSFeedURLString.*?<string>(.*?)</m)[1]
    addRSSNode(body,folders,title,url)
  end
end

Dir.chdir()
File.open('Desktop/AppleMailRSS.opml','w') do |file|
  doc.write( file, 0 )
end

Edit translation
Machine translation (Google):
Loading...
Copy to editor
or Cancel