Last active
March 9, 2018 08:23
-
-
Save knightli/7859214 to your computer and use it in GitHub Desktop.
本script可以配合evernote客户端, 将所选的evernote文章转换为markdown格式存在您的Documents目录下, 同时会自动用TextMate打开该markdown文本. 注意: 并非所有evernote文章都能被正确处理, 本script需要配合markdown2evernote(https://gist.github.com/knightli/7859624)使用.
an apple script to trans evernote content to markdown text. This script takes your currently selected Evernote note and generates a Markdown copy of…
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 | |
| --修改: knight (翻译并增加注释) | |
| --11/7/13 | |
| --原作者: Stephen Margheim | |
| --11/9/13 | |
| --open source | |
| REQUIREMENTS | |
| --Satimage osax plugin | |
| --Aaron Swartz's html2text python script | |
| --Evernote | |
| [脚本的作用] | |
| 本script可以配合evernote客户端, 将所选的evernote文章转换为markdown格式存在您的Documents目录下, 同时会自动用TextMate打开该markdown文本 | |
| [依赖说明] | |
| 1) Satimage osax plugin | |
| 可以在这里下载: https://www.macupdate.com/app/mac/16798/satimage-osax | |
| 2) Aaron Swartz 的html2text python脚本 | |
| html2text是一段python脚本. | |
| 原版在github上的地址: https://github.com/aaronsw/html2text | |
| 需要对原版html2text稍作改动. | |
| 主要修改的地方是把输入的data改为下面这样 | |
| data = sys.argv[1].decode('utf8') | |
| 这个修改的目的是让html2text可以接受shell作为输入 | |
| 这里放了一份knight修改的版本: | |
| https://gist.github.com/knightli/7859110 | |
| *) | |
| --BE SURE TO INSERT THE FILE PATH TO THE HTML2TEXT PYTHON SCRIPT YOU SAVE | |
| --务必将下面一行中的地址改为正确的html2text python脚本位置 | |
| property html2text : "/Users/knightli/workspace/fiddler/markdown-evernote/html2text.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment