Monday, August 24, 2015

Use Markdown in Evernote

New Instructions 

Updated 2015-08-24: textutil needs to be informed of input format and Evernote has changed.



I love Brett Terpstra's Marked, and its integration with Evernote is barebones and cool. Anyway, the instructions are:
  1. Get the everwatch-turbo.rb script and put it in your ~/scripts directory (below or here on GitHub)
  2. Create a directory called ~/evernote-to-marked
  3. Run it in the background by using this in terminal (adjust if your directory is different)
    ~/scripts/everwatch-turbo.rb &
#!/usr/bin/env ruby
# encoding: UTF-8
# everwatch.rb by Brett Terpstra, 2011
# Watch Evernote for updates and put the current content of the editor into a preview file for Marked.app
# <http://markedapp.com>
# Modified by dan@rosenstark.com, 2015-08-24, Evernote 6.0.16
# TO LAUNCH USE (if in ~/scripts):
# ~/scripts/everwatch-turbo.rb &
require 'digest/sha2'
require 'fileutils'
$debug = false
$last_hash = "no hash yet"
def doEverything
trap("SIGINT") { exit }
path = '~/evernote-to-marked'
FileUtils.mkdir_p path
marked_note = File.expand_path("#{path}/EvernoteSnippetInMarkdown.md")
image_path = "#{path}/images"
FileUtils.mkdir_p image_path
# if you want to do images like ![what](/images/who.jpg) uncomment this line
# `sudo ln -s #{image_path} /images`
watch_note = File.new("#{marked_note}",'w')
watch_note.puts "Your markdown will be arriving shortly..."
watch_note.close
`open "#{marked_note}"`
print_and_flush "Waiting." if $debug
while true do # repeat infinitely
doEverythingInner marked_note
sleep 1
end
end
def doEverythingInner marked_note
note = get_note_content_from_evernote
unless note.strip == '' # if we got something back from the AppleScript
hash = (Digest::SHA2.new << note).to_s
if (hash == $last_hash) then
print_and_flush "." if $debug
else
$last_hash = hash
note = note.gsub(/<(\/)?b>/, "**") # do the bold
note = note.gsub("'","APOSTROPHEEE")
note = note.gsub("\\","BACKSLASHEE")
# convert the contents to plain text
txtnote = %x{echo '#{note}'|textutil -stdin -convert txt -stdout -format html}
txtnote = txtnote.gsub("BACKSLASHEE", "\\")
txtnote = txtnote.gsub("APOSTROPHEEE", "'")
txtnote = txtnote.gsub("Â", "") # new Evernote weirdness 2015-08-24
txtnote = txtnote.gsub("\t•","-") # you can use the evernotey bullets
txtnote = txtnote.gsub("\t◦"," -")
txtnote = txtnote.gsub(/\t\d{1,4}\.\t/, "1. ") # use evernote numbering, but only one level
txtnote = txtnote.gsub("\t", " ") # replace tabs
txtnote = txtnote.gsub("\302\240", " ") # some bizarre space-looking character textutil or Evernote uses
txtnote = txtnote.gsub("\t", " ") # all other tabs to spaces
# write the contents to the preview file
watch_note = File.new("#{marked_note}",'w')
watch_note.puts txtnote
watch_note.close
sleep 1
end
end
end
def get_note_content_from_evernote
# get the contents of the post and continued editor screens
note = %x{ osascript <<APPLESCRIPT
tell application "System Events" to set isRunning to the count of (processes whose name is "Evernote")
if (isRunning > 0)
tell application "Evernote"
if selection is not {} then
set the_selection to selection
return HTML content of item 1 of the_selection
else
return ""
end if
end tell
else
return ""
end if
APPLESCRIPT}
note
end
def print_and_flush(str)
print str
$stdout.flush
end
if __FILE__ == $0
doEverything
end