Created
July 24, 2013 04:43
-
-
Save fractaledmind/6068126 to your computer and use it in GitHub Desktop.
Script inserts Markdown Inline Link (as opposed to my other script, that creates Reference Links) into open 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 INLINE LINK TO CHOSEN EVERNOTE NOTE INTO TEXT DOC | |
-- Stephen Margheim | |
-- 14 July 2013 | |
-- open source | |
This script 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 link at the cursor point. | |
*) | |
--Prepatory Material | |
set LF to (ASCII character 10) | |
set LF1 to (ASCII character 32) & (ASCII character 32) & (ASCII character 10) | |
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 | |
--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 inline link | |
set FinalText to "[" & noteName & "]" & "(" & noteURL & ")" | |
end repeat | |
end tell | |
--insert the Markdown link | |
tell application "TextMate" | |
insert FinalText | |
end tell | |
--Get Markdown formatted info for any note | |
else if result = {button returned:"Choose Other"} then | |
--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 inline link | |
set FinalText to "[" & noteName & "]" & "(" & noteURL & ")" | |
--insert the link | |
tell application "TextMate" | |
insert FinalText | |
end tell | |
end if | |
end if | |
end if | |
(* 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 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