Last active
December 27, 2015 20:09
-
-
Save fractaledmind/7383002 to your computer and use it in GitHub Desktop.
REQUIREMENTS
--Satimage osax plugin for Applescript
--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 the new Markdown text file and holds the Markdown itself in the clipboard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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 |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.