-
-
Save knightli/7859624 to your computer and use it in GitHub Desktop.
依赖: multimarkdown (http://fletcherpenney.net/multimarkdown/download/).
作用: 将剪切板内的markdown文本转换后, 给evernote内自动添加一篇指定title和tag的文章.
这个apple script特别适合和evernote2markdown配合使用.(https://gist.github.com/knightli/7859214)
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
| # require: multimarkdown | |
| # you can download from http://fletcherpenney.net/multimarkdown/download/ | |
| # install mac installer and mac support installer | |
| # after you finish the second installer, you can find the following path: | |
| property markdownloc : "/usr/local/bin/multimarkdown" | |
| property tid : AppleScript's text item delimiters | |
| set mdSource to the clipboard | |
| --check for Title metadata | |
| try | |
| if text item 1 of paragraph 1 of mdSource = "#" then | |
| if not text item 2 of paragraph 1 of mdSource = "#" then | |
| set theTitle to text items 3 thru -1 of paragraph 1 of mdSource as string | |
| set theTitle to my trimEnd(theTitle) | |
| set mdSource to my RemoveFromString(mdSource, paragraph 1 of mdSource) | |
| else | |
| set theTitle to my date_format() | |
| set mdSource to mdSource | |
| end if | |
| else if paragraph 1 of mdSource contains "Title:" then | |
| set theTitle to text items 8 thru -1 of paragraph 1 of mdSource as string | |
| set theTitle to my trimEnd(theTitle) | |
| set mdSource to my RemoveFromString(mdSource, paragraph 1 of mdSource) | |
| end if | |
| on error | |
| set theTitle to my date_format() | |
| set mdSource to mdSource | |
| end try | |
| --check of Notebook metadata | |
| try | |
| if text item 1 of paragraph 2 of mdSource = "=" then | |
| set theNotebook to text items 3 thru -1 of paragraph 2 of mdSource as string | |
| set theNotebook to my trimEnd(theNotebook) | |
| set mdSource to my RemoveFromString(mdSource, paragraph 2 of mdSource) | |
| else if paragraph 2 of mdSource contains "Notebook:" then | |
| set theNotebook to text items 11 thru -1 of paragraph 2 of mdSource as string | |
| set theNotebook to my trimEnd(theNotebook) | |
| set mdSource to my RemoveFromString(mdSource, paragraph 2 of mdSource) | |
| else | |
| set theNotebook to my find_default_notebook() | |
| set mdSource to mdSource | |
| end if | |
| on error | |
| set theNotebook to my find_default_notebook() | |
| set mdSource to mdSource | |
| end try | |
| --check for Tags metadata | |
| try | |
| if text item 1 of paragraph 3 of mdSource = "@" then | |
| set theTags to text items 3 thru -1 of paragraph 3 of mdSource as string | |
| set theTags to my trimEnd(theTags) | |
| set AppleScript's text item delimiters to ", " | |
| set theTags to text items of theTags | |
| set AppleScript's text item delimiters to tid | |
| set mdSource to my RemoveFromString(mdSource, paragraph 3 of mdSource) | |
| else if paragraph 3 of mdSource contains "Keywords:" then | |
| set theTags to text items 11 thru -1 of paragraph 3 of mdSource as string | |
| set theTags to my trimEnd(theTags) | |
| set AppleScript's text item delimiters to ", " | |
| set theTags to text items of theTags | |
| set AppleScript's text item delimiters to tid | |
| set mdSource to my RemoveFromString(mdSource, paragraph 3 of mdSource) | |
| else | |
| set theTags to "" | |
| set mdSource to mdSource | |
| end if | |
| on error | |
| set theTags to "" | |
| set mdSource to mdSource | |
| end try | |
| --convert (Multi)Markdown to HTML | |
| set rawHTML to do shell script "echo " & the quoted form of mdSource & " | " & markdownloc | |
| --create new Evernote note | |
| tell application id "com.evernote.Evernote" | |
| if (not (notebook named theNotebook exists)) then | |
| make notebook with properties {name:theNotebook} | |
| end if | |
| set N to create note title theTitle with html rawHTML notebook theNotebook | |
| repeat with i from 1 to count of theTags | |
| set x to item i of theTags | |
| if (not (tag named x exists)) then | |
| set tag1 to make tag with properties {name:x} | |
| else | |
| set tag1 to tag x | |
| end if | |
| assign tag1 to N | |
| end repeat | |
| end tell | |
| (* HANDLERS *) | |
| on trimEnd(str) | |
| -- HAS (http://applemods.sourceforge.net/mods/Data/String.php) | |
| local str, whiteSpace | |
| try | |
| set str to str as string | |
| set whiteSpace to {character id 10, return, space, tab} | |
| try | |
| repeat while str's last character is in whiteSpace | |
| set str to str's text 1 thru -2 | |
| end repeat | |
| return str | |
| on error number -1728 | |
| return "" | |
| end try | |
| on error eMsg number eNum | |
| error "Can't trimEnd: " & eMsg number eNum | |
| end try | |
| end trimEnd | |
| to date_format() -- Old_date is text, not a date. | |
| set {year:y, month:m, day:d} to (current date) | |
| tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8 | |
| end date_format | |
| on find_default_notebook() | |
| tell application id "com.evernote.Evernote" | |
| set enNotebooks to every notebook | |
| set defaultNB to "" | |
| repeat with i from 1 to count of enNotebooks | |
| set theNB to item i of enNotebooks | |
| if theNB is default then | |
| set nbName to name of theNB | |
| set defaultNB to defaultNB & nbName | |
| end if | |
| end repeat | |
| return defaultNB | |
| end tell | |
| end find_default_notebook | |
| on RemoveFromString(theText, CharOrString) | |
| -- ljr (http://applescript.bratis-lover.net/library/string/) | |
| local ASTID, theText, CharOrString, lst | |
| set ASTID to AppleScript's text item delimiters | |
| try | |
| considering case | |
| if theText does not contain CharOrString then ¬ | |
| return theText | |
| set AppleScript's text item delimiters to CharOrString | |
| set lst to theText's text items | |
| end considering | |
| set AppleScript's text item delimiters to ASTID | |
| return lst as text | |
| on error eMsg number eNum | |
| set AppleScript's text item delimiters to ASTID | |
| error "Can't RemoveFromString: " & eMsg number eNum | |
| end try | |
| end RemoveFromString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment