Created
July 24, 2013 04:42
-
-
Save fractaledmind/6068122 to your computer and use it in GitHub Desktop.
Script inserts link to either selected note or chosen note in Evernote.app. This scripts creates Markdown Reference Links in either TextEdit or TextMate document.
This file contains 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
(* INSERT MARKDOWN LINK TO CHOSEN EVERNOTE NOTE INTO TEXTEDIT DOC | |
-- Stephen Margheim | |
-- 14 July 2013 | |
-- open source | |
This script assumes you are writing in an open TextEdit document. It will prompt you either (1) to get the note link of whatever note is currently selected in your Evernote app or (2) to choose a specific note and insert its link. Whichever you choose, the script will insert the note title at the cursor point along with a randomly generated 2-digit number and a letter of the alphabet as the link ID. Then, at the end of the document that same link ID and the Evernote URL are inserted. The cursor is placed one space after the title link. So, for example, you would have something like this: | |
"Some text [title][ID] (then type some more text) | |
[ID]: Evernote URL | |
" | |
Usage notes: | |
-- You must have at least two extra lines at the end of your text document, or else the [ID]: URL will be appended right after your [title][ID] and you will need to cut and paste. I typically begin every document by hitting Return 3 times then returning to the beginning to actually start typing. | |
*) | |
--if you use TextMate, change this property to "TextMate" and be sure you have an open, empty TextMate file | |
property theEditor : "TextEdit" | |
--Prepatory Material | |
set LF to (ASCII character 10) | |
set LF1 to (ASCII character 32) & (ASCII character 32) & (ASCII character 10) | |
set theAlphabet to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} | |
display dialog "Get Link of selected note or choose other note?" buttons {"Get Selected", "Choose Other"} default button {"Get Selected"} | |
--Get Markdown formatted info from Selected Note | |
if result = {button returned:"Get Selected"} then | |
--prepare the random Link ID | |
set randomLetter to random number from 1 to 26 | |
set randomLetter to item randomLetter of theAlphabet | |
set theNum1 to random number from 1 to 9 | |
set theNum2 to random number from 1 to 9 | |
set LinkID to "1" & theNum1 & theNum2 & randomLetter | |
--get the note link of the selected note | |
tell application "Evernote" | |
set Evernote_Selection to selection | |
if Evernote_Selection is {} then display dialog "Please select a note." | |
set noteLink to "" | |
repeat with i from 1 to the count of Evernote_Selection | |
set noteURL to note link of item i of Evernote_Selection | |
set noteName to title of item i of Evernote_Selection | |
--prepare Markdown reference link | |
set InsertText to "[" & noteName & "]" & "[" & LinkID & "]" | |
set the clipboard to InsertText | |
set FinalText to "[" & LinkID & "]: " & noteURL & LF1 | |
end repeat | |
end tell | |
if theEditor is "TextEdit" then | |
--insert the anchor at the cursor point and put the reference link at the end of the document | |
tell application "TextEdit" | |
activate | |
tell application "System Events" | |
keystroke "v" using {command down} | |
end tell | |
delay 0.1 | |
make new paragraph at end of document 1 with data FinalText | |
activate | |
end tell | |
else if theEditor is "TextMate" then | |
tell application "TextMate" | |
insert InsertText & LF1 & LF & LF & FinalText | |
end tell | |
end if | |
--Get Markdown formatted info for any note | |
else if result = {button returned:"Choose Other"} then | |
--prepare randown Link ID | |
set randomLetter to random number from 1 to 26 | |
set randomLetter to item randomLetter of theAlphabet | |
set theNum1 to random number from 1 to 9 | |
set theNum2 to random number from 1 to 9 | |
set LinkID to "1" & theNum1 & theNum2 & randomLetter | |
--present list of Notebooks in your account | |
tell application "Evernote" | |
set listOfNotebooks to {} | |
set EVNotebooks to every notebook | |
repeat with currentNotebook in EVNotebooks | |
set currentNotebookName to (the name of currentNotebook) | |
copy currentNotebookName to the end of listOfNotebooks | |
end repeat | |
set Folders_sorted to my simple_sort(listOfNotebooks) | |
end tell | |
set SelNotebook to choose from list of Folders_sorted with title "Select Evernote Notebook" with prompt ¬ | |
"Current Evernote Notebooks" OK button name "OK" | |
if SelNotebook is false then | |
beep | |
else | |
set EVnotebook to item 1 of SelNotebook | |
set listofNotes to {} | |
set listofLinks to {} | |
--present list of notes in the selected notebook and get links for each | |
tell application "Evernote" | |
set allNotes to every note in notebook EVnotebook | |
repeat with currentNote in allNotes | |
set currentNoteName to (the title of currentNote) | |
copy currentNoteName to the end of listofNotes | |
set currentNoteURL to (the note link of currentNote) | |
copy currentNoteURL to the end of listofLinks | |
end repeat | |
end tell | |
set SelNote to ¬ | |
choose from list of listofNotes with title ¬ | |
"List of Notes" OK button name "Get Link" cancel button name "Close Window" | |
if SelNote is false then | |
beep | |
else | |
--get link info for selected note | |
set noteName to SelNote as string | |
set notePlacement to my findAll(listofNotes, noteName) as number | |
set noteURL to item notePlacement of listofLinks | |
--prepare Markdown reference link | |
set InsertText to "[" & noteName & "]" & "[" & LinkID & "]" | |
set the clipboard to InsertText | |
set FinalText to "[" & LinkID & "]: " & noteURL & LF1 | |
--insert the anchor at the cursor point and put the reference link at the end of the document | |
tell application "TextEdit" | |
activate | |
tell application "System Events" | |
keystroke "v" using {command down} | |
end tell | |
delay 0.1 | |
make new paragraph at end of document 1 with data FinalText | |
activate | |
end tell | |
end if | |
end if | |
end if | |
(* SUBROUTINES *) | |
--SORT SUBROUTINE | |
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 findAll(lst, val) | |
-- HAS (http://applemods.sourceforge.net/mods/Data/List.php) | |
local lst, val, res | |
try | |
if lst's class is not list then error "not a list." number -1704 | |
if {val} is not in lst then return {} | |
set res to {} | |
script k | |
property l : lst | |
end script | |
repeat with i from 1 to count of k's l | |
if k's l's item i is val then set res's end to i | |
end repeat | |
return res | |
on error eMsg number eNum | |
error "Can't find All: " & eMsg number eNum | |
end try | |
end findAll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment