A better System Service for Evernote clipping - with MultiMarkdown

[Tweet : ADN : nvALT]

Update: If you’re looking for a Markdown -> Evernote, check this out.

Another post, quickly and with less explanation…

Evernote IconThe fact that Evernote processes HTML so much better than it does plain or rich text got me thinking and tinkering. I use Markdown (actually, MultiMarkdown) constantly, and it does a great job of turning plain text into valid markup. With (Multi)Markdown, even plain text becomes HTML that–when imported into Evernote–retains most of its formatting. To answer your question, no, I’m not obsessed with Evernote, I’m obsessed with problems I think I could solve. It’s unhealthy.

Please note, this requires that you have Fletcher Penney’s MultiMarkdown installed in ~/Library/Application Support/MultiMarkdown, and that the Perl files (MultiMarkdown.pl and SmartyPants.pl) are located in a ‘bin’ subdirectory (which is the default install). If you don’t have MultiMarkdown, you should get it anyway (all the cool kids have it), so head over to the download page and grab a copy. Now, on with the show.

I set this up originally as a TextMate command, intending just to be able to clip code snippets and free-form text to Evernote without thinking too much about it. That worked well, so I modified it to work as a System Service. Specifically, a Snow Leopard service, but I’m providing the Ruby script here and it can be modified for any Mac setup you want.

While it will work just fine on plain text with no markup, it does have a couple of “special” features. If you start a line with a # and a space (e.g.: # This is my header), which is a Markdown convention for a first-level heading, it will use that as the title for the note and strip it out of the text in processing. It only uses the first one it finds, but it will strip out any first-level headers in the selection. I’ll probably modify that later, or just have it leave them in. Also, a line that begins with “tags:” followed by a space and a comma-separated list of words will be split up and used to tag the new note. This is also stripped before processing. It handles spaces in multi-word tags, and odd marks at the beginning or end of a tag, but only one punctuation character, and only at the beginning or end of a tag. The code follows…

Here’s the Ruby code, messy as it may be:

#!/usr/bin/env ruby -rjcode -Ku
# requires that MultiMarkdown be installed in ~/Library/Application Support/MultiMarkdown
# That, or edit the script to point to yours :)

ARGF.each do |f|
input = f
contents = ''
tags = ''
title = nil

def e_as(str)
	str.to_s.gsub(/(?=["\\])/, '\\')
input.each_line { |line| 
  if line =~ /^# (.*?)/
    title = line[2..-1]
title = %x{date '+Clipped note: %A, %B %d, %Y at %l:%M %p'|tr -s " "} if title.nil?

input.each_line { |line| 
  if line =~ /^[Tt]ags: /
    tags = line[5..-1].split(',').map {|tag| tag = tag.strip.gsub(/^(.)?\b|\b(.)?$/,"\\2\"\\1") }

IO.popen('"$HOME/Library/Application Support/MultiMarkdown/bin/MultiMarkdown.pl"|"$HOME/Library/Application Support/MultiMarkdown/bin/SmartyPants.pl"', "r+") do |io|
 input.each_line { |line| 
	io << line unless line =~ /^# |[Tt]ags\: /
 }; io.close_write
 io.each_line do |line|
   contents << line
tags = " tags {#{tags.join(",")}}" unless tags.empty?
%x{osascript -e 'tell application "Evernote" to create note with html "#{e_as contents}" title "#{title}" notebook "Unfiled"#{tags}'}

You can create a System Service in Automator with it, set up a command in TextMate, or do whatever else you can think of. If you just want to download the service and try it out, I’ve made it available here. Unzip and copy it to ~/Library/Services (in your home folder). If you set it up as a System Service, assign a shortcut key in the Keyboard pane of System Preferences.

It does choke once in a while, apparently on Markdown-generated code snippets, but I haven’t quite narrowed down why, yet. I’ll update the code if I figure that one out. Overall, though, it makes pretty clippings and allows you to use some Markdown syntax to spice up your text without having to touch the (regrettably abominable) Evernote editor.

Markdown To Evernote Service v1.0

A Snow Leopard System Service that grabs selected text, processes it with MultiMarkdown and clips the resulting HTML to Evernote, creating a nicely formatted note. Uses the first (and hopefully only) Markdown first-level heading (# My headline) as the title for the note, and will look for a line starting with “tags: ” followed by a comma-separated list of tags as well. Requires that MultiMarkdown be installed in ~/Library/Application Support/MultiMarkdown.

Updated Sat Mar 06 2010.

More info…