Created
September 2, 2013 05:16
-
-
Save fractaledmind/6409397 to your computer and use it in GitHub Desktop.
This script creates a new Note in the user's default notebook with hyperlinks to all of the notes with a given Tag. Thus, it creates a Tag Index note for a chosen 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
| (* Create Tag Index Note With Hyperlinks to All Notes with that Tag *) | |
| property LF : ASCII character 10 | |
| tell application "Evernote" | |
| set noteTags to every tag | |
| set theTags to {} | |
| repeat with i from 1 to count of noteTags | |
| set theName to name of item i of noteTags | |
| copy theName to end of theTags | |
| end repeat | |
| set theTags to my removeDuplicates(theTags) | |
| set theTags to my simple_sort(theTags) | |
| set thisTag to choose from list theTags with title "Create Index Note for which Tag?" | |
| set thisTag to thisTag as string | |
| set matches to find notes "tag: " & thisTag | |
| set tagNotes_markdown to "" | |
| repeat with i from 1 to count of matches | |
| set theTitle to title of item i of matches | |
| set theLink to note link of item i of matches | |
| set wikiLink to "<p><a href=\"" & theLink & "\">" & theTitle & "</a></p>" & LF & LF | |
| set tagNotes_markdown to tagNotes_markdown & wikiLink | |
| end repeat | |
| set nbName to my find_default_notebook() | |
| set newNote to create note title "! " & thisTag & " Index" with html tagNotes_markdown notebook nbName | |
| end tell | |
| (* HANDLERS *) | |
| 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_default_notebook() | |
| tell application "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 removeDuplicates(lst) | |
| -- HAS (http://applemods.sourceforge.net/mods/Data/List.php) | |
| local lst, itemRef, res, itm | |
| try | |
| if lst's class is not list then error "not a list." number -1704 | |
| script k | |
| property l : lst | |
| property res : {} | |
| end script | |
| repeat with itemRef in k's l | |
| set itm to itemRef's contents | |
| -- note: minor speed optimisation when removing duplicates | |
| -- from ordered lists: assemble new list in reverse so | |
| -- 'contains' operator checks most recent item first | |
| if k's res does not contain {itm} then ¬ | |
| set k's res's beginning to itm | |
| end repeat | |
| return k's res's reverse | |
| on error eMsg number eNum | |
| error "Can't removeDuplicates: " & eMsg number eNum | |
| end try | |
| end removeDuplicates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For me, it produced a new note with hyperlinks to all search results of that tag name...