-
-
Save fractaledmind/7383002 to your computer and use it in GitHub Desktop.
(* EVERNOTE TO MARKDOWN | |
--Stephen Margheim | |
--11/9/13 | |
--open source | |
REQUIREMENTS | |
--Satimage osax plugin | |
--Aaron Swartz's html2text python script | |
--Evernote | |
This script takes your currently selected Evernote note and generates a Markdown copy of it in your Documents folder. It will then automatically open this Markdown text in TextMate. | |
The script requires the html2text script to function. Once you find and save the script (you can grab the code out of Brett Terpstra's "md - Convert - HTML to Markdown" service. Simply open that in Automator and copy out the code), be sure that you also change the second to last line of the code to: | |
data = sys.argv[1].decode('utf8') | |
This allows the html2text script to accept input from the shell command. Where ever you save the new python code, be sure to alter the html2text property below with its file path. | |
*) | |
--BE SURE TO INSERT THE FILE PATH TO THE HTML2TEXT PYTHON SCRIPT YOU SAVE | |
property html2text : "/Users/[username/Path/to/HTML2Md.py" | |
property LF : (ASCII character 32) & (ASCII character 32) & (ASCII character 10) | |
property DocsFolder : POSIX path of (path to documents folder) | |
on run | |
if is_running("Evernote") = false then | |
tell application id "com.evernote.Evernote" to launch | |
end if | |
tell application id "com.evernote.Evernote" | |
set Evernote_Selection to selection | |
if Evernote_Selection is {} then display dialog "Please select a note." | |
repeat with i from 1 to the count of Evernote_Selection | |
set theTitle to title of item i of Evernote_Selection | |
set theNotebook to name of notebook of item i of Evernote_Selection | |
set theTags to tags of item i of Evernote_Selection | |
set noteTags_list to {} | |
repeat with j from 1 to count of theTags | |
set tagName to name of item j of theTags | |
copy tagName to the end of noteTags_list | |
end repeat | |
set theTags to my joinList(noteTags_list, ", ") | |
set noteHTML to HTML content of item i of Evernote_Selection | |
end repeat | |
end tell | |
set noteHTML to change "\\<body style(.*?)\\>" into "" in noteHTML with regexp | |
set noteHTML to change "\\<\\?xml(.*?)\\>" into "" in noteHTML with regexp | |
set noteHTML to change "\\<\\!DOC(.*?)\\>" into "" in noteHTML with regexp | |
set noteHTML to change "\\<html(.*?)\\>" into "" in noteHTML with regexp | |
set noteHTML to change "\\<span style(.*?)\\>" into "" in noteHTML with regexp | |
try | |
set theMarkdown to do shell script "python " & html2text & " \"" & noteHTML & "\"" | |
set theMarkdown to "# " & theTitle & " " & LF & "= " & theNotebook & " " & LF & "@ " & theTags & " " & LF & LF & LF & theMarkdown | |
set the clipboard to theMarkdown | |
--create and open new note | |
if theTitle contains "/" then | |
set theTitle to my replaceString(theTitle, "/", "-") | |
end if | |
set shellTitle to quoted form of theTitle | |
do shell script "cd " & DocsFolder & "; mkdir -p Evernote_Markdown_Notes; cd Evernote_Markdown_Notes/; touch " & shellTitle & ".md; open -a TextMate " & shellTitle & ".md" | |
tell application "System Events" | |
tell process "TextMate" | |
activate | |
delay 0.1 | |
keystroke "v" using {command down} | |
delay 0.1 | |
keystroke "s" using {command down} | |
end tell | |
end tell | |
on error errStr number errorNumber | |
error errStr number errorNumber | |
end try | |
end run | |
(* HANDLERS *) | |
on is_running(appName) | |
tell application "System Events" to (name of processes) contains appName | |
end is_running | |
to joinList(aList, delimiter) | |
set retVal to "" | |
set prevDelimiter to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to delimiter | |
set retVal to aList as string | |
set AppleScript's text item delimiters to prevDelimiter | |
return retVal | |
end joinList | |
on replaceString(theText, oldString, newString) | |
-- ljr (http://applescript.bratis-lover.net/library/string/) | |
local ASTID, theText, oldString, newString, lst | |
set ASTID to AppleScript's text item delimiters | |
try | |
considering case | |
set AppleScript's text item delimiters to oldString | |
set lst to every text item of theText | |
set AppleScript's text item delimiters to newString | |
set theText to lst as string | |
end considering | |
set AppleScript's text item delimiters to ASTID | |
return theText | |
on error eMsg number eNum | |
set AppleScript's text item delimiters to ASTID | |
error "Can't replaceString: " & eMsg number eNum | |
end try | |
end replaceString |
Also, some of those errors come from HTML metadata in more complex notes. The reason I have line 46 in the script is that the html2md parser cannot handle the HTML metadata. I looked at one of my more complex notes (embedded images) and found this in addition to the standard metadata:
You will see that this corresponds to some of the errors you received.Thanks for the quick response,
I tried running the script with a new, simple text note and received the following error:
https://www.dropbox.com/s/jpu4oiwlnrqjdpe/Text%20only%20note%20in%20Evernote%20Error%20%20.tiff
The screenshot of the actual note selected in Evernote (The font used is Palatino 18):
https://www.dropbox.com/s/e500xur61lt5ypa/Text%20only%20note%20selected%20in%20Evernote.jpg
Ok. I've isolated the problem. It appears that in a recent Evernote update, they updated the data they include in the note's HTML. This data can't be parsed by the html2text script. So the solution is to remove it (it is unessential to the content of the note) before we run the parser. My solution utilizes the Satimage osax plugin for Applescript. I wrote about it here: http://hackademic.postach.io/applescript-essential. You'll need it for the upgraded script to function. But there shouldn't be any errors on text-based Evernote notes.
Excellent! One minor annoyance is that the script only outputs the markdown file if my text editor (TextWrangler in my case) is running. (if not it creates an empty markdown file with the note's title as a file name) Other than that, I'm glad to say it runs smoothly.
Add the:
if is_running("Evernote") = false then
tell application id "com.evernote.Evernote" to launch
end if
block for your text editor. At the beginning of the script, check that Evernote is running and your text editor is running. If either one isn't, launch them before the rest of the script continues. That should solve the problem.
Hmm...This is an odd error. It is clearly an error from the html2md script run. Here are my initial thoughts:
The html2md script isn't error-proof, it is expecting standard html text content. Evernote (and its ENML format) can of course handle much more than simple text notes. I doubt that the script will understand or translate such notes (notes with images, notes clipped from web-pages, notes with audio, etc).
Your set up appears fine, so I would imagine its a problem with your Evernote note (not the note itself, but it may be too complex to render in simple Markdown). For me to test this hypothesis, I'd need to see what your Evernote note looks like. Could you test the script on text only Evernote notes? Does it work then?