Created
August 30, 2013 17:22
-
-
Save fractaledmind/6392245 to your computer and use it in GitHub Desktop.
This script is application agnostic. It works from the clipboard and returns text to the clipboard. It will take text, extract any words or phrases in [[double brackets]] and make new Evernote notes with those terms as titles. The script assumes you have put MMD meta-data at the beginning of the text in this format:
# Note Title
= Notebook
@ tag…
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
| set theText to (the clipboard as text) | |
| set tid to AppleScript's text item delimiters | |
| set AppleScript's text item delimiters to "" | |
| set theCharacters to text items of theText | |
| set AppleScript's text item delimiters to tid | |
| set p to paragraphs of theText | |
| set noteName to {} | |
| repeat with i from 1 to count of p | |
| if p's item i begins with "# " then set noteName's end to p's item i | |
| end repeat | |
| set noteName to (noteName as text) | |
| set noteName to my RemoveFromString(noteName, "# ") | |
| set theUnBracket_list to my get_text(theCharacters, "[", "]") | |
| set theBracket_list to my rebind_list(theUnBracket_list, "[[", "]]") | |
| --search for and create notes from above list | |
| tell application "Evernote" | |
| set tid to AppleScript's text item delimiters | |
| if (not (notebook named noteName exists)) then | |
| make notebook with properties {name:noteName} | |
| set notebook1 to notebook noteName | |
| set notebook_search to name of notebook1 | |
| else | |
| set notebook1 to notebook noteName | |
| set notebook_search to name of notebook1 | |
| end if | |
| set Wiki_list to theUnBracket_list | |
| set Wiki_list_count to (count items of Wiki_list) as number | |
| repeat with i from 1 to Wiki_list_count | |
| set Wiki_word to item i of Wiki_list | |
| set matches to find notes "intitle:" & Wiki_word & " " & "tag:Wiki" & " " & "notebook:\"" & notebook_search & "\"" | |
| if matches = {} then | |
| set Wiki_note to create note title Wiki_word with text "" notebook notebook1 | |
| if (not (tag named "Wiki" exists)) then | |
| make tag with properties {name:"Wiki"} | |
| end if | |
| set tag1 to tag "Wiki" | |
| assign tag1 to Wiki_note | |
| end if | |
| if (i > Wiki_list_count) then | |
| exit repeat | |
| end if | |
| end repeat | |
| --All sub notes are now created with appropriate tag and title | |
| --Next, synchronize to get note links for all wiki notes | |
| repeat until isSynchronizing is false | |
| end repeat | |
| synchronize | |
| repeat until isSynchronizing is false | |
| end repeat | |
| end tell | |
| --get note links for all wiki notes | |
| tell application "Evernote" | |
| set Evernote_Selection to every note of notebook1 | |
| set Wiki_links_list to {} | |
| repeat with i from 1 to the count of Evernote_Selection | |
| set noteURL to note link of item i of Evernote_Selection | |
| set noteTitle to title of item i of Evernote_Selection | |
| set theWikiLink to "[" & noteTitle & "]" & "(" & noteURL & ")" | |
| copy theWikiLink to beginning of Wiki_links_list | |
| end repeat | |
| end tell | |
| set bracket_list to my simple_sort(theBracket_list) | |
| set Wiki_list to my simple_sort(Wiki_links_list) | |
| set newText to my find_replace_repeated(theText, bracket_list, Wiki_list) | |
| return newText | |
| (* Handlers *) | |
| on get_text(this_list, frontChar, backChar) | |
| set indexList to {} | |
| repeat with i from 1 to the count of this_list | |
| if item i of this_list is frontChar then | |
| if item (i + 1) of this_list is frontChar then | |
| copy (i + 2) to end of indexList | |
| end if | |
| end if | |
| if item i of this_list is backChar then | |
| if item (i + 1) of this_list is backChar then | |
| copy (i - 1) to end of indexList | |
| end if | |
| end if | |
| end repeat | |
| set indexList to my groupList(indexList, 2) | |
| set text_list to {} | |
| repeat with i from 1 to count of indexList | |
| set theFront to item 1 of item i of indexList | |
| set theBack to item 2 of item i of indexList | |
| set theText to items theFront thru theBack of this_list as string | |
| copy theText to end of text_list | |
| end repeat | |
| return text_list | |
| end get_text | |
| on rebind_list(this_list, frontChars, backChars) | |
| set bracket_list to {} | |
| repeat with i from 1 to the count of this_list | |
| set itemName to item i of this_list | |
| set theBracketName to "[[" & itemName & "]]" | |
| copy theBracketName to end of bracket_list | |
| end repeat | |
| return bracket_list | |
| end rebind_list | |
| 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 | |
| set newText to lst as text | |
| set newText to my trimEnd(newText) | |
| return newText | |
| on error eMsg number eNum | |
| set AppleScript's text item delimiters to ASTID | |
| error "Can't RemoveFromString: " & eMsg number eNum | |
| end try | |
| end RemoveFromString | |
| on simple_sort(my_list) | |
| set the index_list to {} | |
| set the sorted_list to {} | |
| repeat (the number of items in my_list) times | |
| set the low_item to "" | |
| repeat with i from 1 to (number of items in my_list) | |
| if i is not in the index_list then | |
| set this_item to item i of my_list as text | |
| if the low_item is "" then | |
| set the low_item to this_item | |
| set the low_item_index to i | |
| else if this_item comes before the low_item then | |
| set the low_item to this_item | |
| set the low_item_index to i | |
| end if | |
| end if | |
| end repeat | |
| set the end of sorted_list to the low_item | |
| set the end of the index_list to the low_item_index | |
| end repeat | |
| return the sorted_list | |
| end simple_sort | |
| on find_replace_repeated(the_string, search_strings, replace_strings) | |
| set ListNumber to the (count of search_strings) as number | |
| set OldDelims to AppleScript's text item delimiters | |
| considering case | |
| repeat with i from 1 to ListNumber | |
| set AppleScript's text item delimiters to item i of search_strings | |
| set newText to text items of the_string | |
| set AppleScript's text item delimiters to item i of replace_strings | |
| set the_string to newText as text | |
| set AppleScript's text item delimiters to OldDelims | |
| end repeat | |
| end considering | |
| return the_string | |
| end find_replace_repeated | |
| (* Sub-Routines *) | |
| on groupList(lst, groupLen) | |
| -- HAS (http://applemods.sourceforge.net/mods/Data/List.php) | |
| local lst, tailLen, groupLen, idx | |
| try | |
| if lst's class is not list then error "not a list." number -1704 | |
| script k | |
| property l : lst | |
| property res : {} | |
| end script | |
| set tailLen to (count of k's l) mod groupLen | |
| repeat with idx from 1 to ((count of k's l) - tailLen) by groupLen | |
| set k's res's end to k's l's items idx thru (idx + groupLen - 1) | |
| end repeat | |
| if tailLen is not 0 then | |
| set k's res's end to k's l's items -tailLen thru -1 | |
| end if | |
| return k's res | |
| on error eMsg number eNum | |
| error "Can't groupList: " & eMsg number eNum | |
| end try | |
| end groupList | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I notice the
rebind_listhandler has the brackets hard-coded rather than using the passed parameters. Was there a problem using the parameters, or is this just an oversight. (I'm not currently near my Mac, otherwise I'd check for myself :-))