Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fractaledmind/6258839 to your computer and use it in GitHub Desktop.
Save fractaledmind/6258839 to your computer and use it in GitHub Desktop.
// FOR THE FREE TEXTEDIT APP. // This script searches for matches in the current EN note to pre-existing Wiki notes and links to them. It thus mimics the autolinking functionality of personal wikis, such as Voodoopad. To use, you simply either type up a note or go to an existing note and run the script. The script functions by [1] getting the te…
tell application "Evernote"
try
set Evernote_Selection to selection
if Evernote_Selection = {} then
display dialog "Please select the note to Wikify"
end if
set noteName to (title of item 1 of Evernote_Selection)
set notebookName to (name of notebook of item 1 of Evernote_Selection)
end try
--get plain text of note; from Justin's Veritrope script
set the_HTML to HTML content of (item 1 of Evernote_Selection)
set plain_Text to do shell script "echo " & quoted form of the_HTML & space & "| textutil -convert txt -stdin -stdout"
end tell
--search Evernote for all previously existing Wiki notes and get list of titles
tell application "Evernote"
set WikiSearch to find notes "tag:Wiki"
set listofNotes to {}
set allNotes to every item in WikiSearch
repeat with currentNote in allNotes
set currentNoteName to (the title of currentNote)
copy currentNoteName to the end of listofNotes
end repeat
end tell
--put [[brackets]] around the search terms from the list of Wiki EN notes; for find and replace later
set PreBracket_list to every item of listofNotes
set the clipboard to ""
repeat with i from 1 to the count of PreBracket_list
set itemName to item i of PreBracket_list
set theBracketName to "[[" & itemName & "]]" & return
set the clipboard to (the clipboard) & theBracketName
end repeat
set Bracketed_search_terms to the clipboard
set Bracketed_list to get paragraphs of Bracketed_search_terms
--delete any duplicate WikiWords in your note from the List
set New_Bracket_list to my remove_duplicates(Bracketed_list)
set New_listofNotes to my remove_duplicates(listofNotes)
--sort both lists to ensure all items are in the same order
set Next_Bracket_list to my simple_sort(New_Bracket_list)
set Next_listofNotes to my simple_sort(New_listofNotes)
--remove the empty item from both lists
set EmptyItem to {""}
set Final_Bracket_list to my remove_emptyitem(Next_Bracket_list, EmptyItem)
set Final_listofNotes to my remove_emptyitem(Next_listofNotes, EmptyItem)
--put [[brackets]] around all instances of search terms in note via find and replace
set the_string to plain_Text
set search_strings to Final_listofNotes
set replace_strings to Final_Bracket_list
set ListNumber to the (count of search_strings) as number
set OldDelims to AppleScript's text item delimiters
set the clipboard to ""
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 the clipboard to the_string
set AppleScript's text item delimiters to OldDelims
end repeat
end considering
set AlmostFinalText to the clipboard
--searches new text and extracts all words within brackets
extractBetween(AlmostFinalText, "[[", "]]")
set UnBracketed_text to the clipboard
--puts item in list form
set UnBracketed_list to get words of UnBracketed_text
--delete any duplicate WikiWords in your note from the List
set Unique_UnBracket_list to my remove_duplicates(UnBracketed_list)
--search for pre-existing Wiki notes from above list and get URLS
tell application "Evernote"
set tid to AppleScript's text item delimiters
set the clipboard to ""
set Wiki_list to Unique_UnBracket_list
set Wiki_list_count to (count items of Wiki_list) as number
set i to 1
repeat with i from 1 to Wiki_list_count
set Wiki_word to item i of Wiki_list
set query string of window 1 to "intitle:" & Wiki_word & " " & "tag:Wiki"
delay 0.5
set noteURL to note link of note 1 of window 1
set noteTitle to title of note 1 of window 1
set theWikiLink to "[" & noteTitle & "]" & "(" & noteURL & ")" & return
set the clipboard to (the clipboard) & theWikiLink
if (i > Wiki_list_count) then
exit repeat
end if
end repeat
set WikiLinks_text to the clipboard
delay 0.5
set query string of window 1 to "intitle:" & noteName
end tell
set WikiLinks_list to get paragraphs of WikiLinks_text
--this variable takes your list of Wiki-Markdown links and puts into list
set Final_WikiLinks_list to my remove_emptyitem(WikiLinks_list, EmptyItem)
--put [[brackets]] back around the terms from the earlier list of pre-existing notes; for find and replace later
set the clipboard to ""
repeat with i from 1 to the count of Unique_UnBracket_list
set itemName to item i of Unique_UnBracket_list
set theBracketName to "[[" & itemName & "]]" & return
set the clipboard to (the clipboard) & theBracketName
end repeat
set NewBracketed_text to the clipboard
--this variable has a simple string of your WikiWords
set Next_Bracketed_list to get paragraphs of NewBracketed_text
set Final_Bracketed_list to my remove_emptyitem(Next_Bracketed_list, EmptyItem)
--find and replace [[bracketed]] terms in the text with Markdown [title](link)
set the_string to AlmostFinalText
set search_strings to Final_Bracketed_list
set replace_strings to Final_WikiLinks_list
considering case
try
set ListNumber to the (count of search_strings) as number
set OldDelims to AppleScript's text item delimiters
set the clipboard to ""
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 the clipboard to the_string
set AppleScript's text item delimiters to OldDelims
end repeat
end try
end considering
set FinalText to (the clipboard)
--establish a variable for the line feed (aka LF key)
set LF to ASCII character 10
set FinalText to "Title: " & noteName & LF & "Notebook: " & notebookName & LF & "Keywords: Wiki" & LF & LF & FinalText
--Send Markdown text to TextEdit. TextEdit must have a clean text document open.
tell application "TextEdit"
make new word at end of text of document 1 with data FinalText
end tell
--delete old note
tell application "Evernote"
set FinalSearch to find notes "intitle:" & noteName & " " & "-tag:Wiki"
delete FinalSearch
end tell
(* Here, the user can either manually export to EN. To export, you first need to have Multimarkdown installed, and get the improved Markdown2Evernote script from https://gist.github.com/kopischke/1009149. You then set this up as a Command in TextMate. Tim Lockridge has a good set-by-step for the setup here: http://blog.timlockridge.com/blog/2013/02/03/using-textmate-and-markdown-with-evernote-configuring-a-workflow/.
To export, go to Bundles -> Markdown _> Markdown2Evernote.
OR, you could use this could to try and automate the export
--send Markdown formatted text to Evernote
tell application "TextMate" to activate
menu_click({"TextMate", "Bundles", "Markdown", "Markdown2Evernote"})
*)
(* SUBROUTINES *)
--handler from Qwerty Denzel on MacScripter
on remove_duplicates(this_text)
set not_list to class of this_text is not list
if not_list then set this_text to paragraphs of this_text
set new_text to {}
repeat with this_line in this_text
if this_line is not in new_text then set end of new_text to (contents of this_line)
end repeat
if not_list then
set text item delimiters to return
tell new_text to set new_text to beginning & ({""} & rest)
set text item delimiters to ""
end if
return new_text
end remove_duplicates
--edited from julifos' script on MacScripter
on remove_emptyitem(start_list, remove_item)
set theList to start_list
set itemsToDelete to remove_item
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "TRICK"
set theList to "TRICK" & theList & "TRICK"
--> theList = "TRICKabTRICKbcTRICKcdTRICKdeTRICK"
repeat with i in itemsToDelete
set AppleScript's text item delimiters to "TRICK" & i & "TRICK"
set theList to theList's text items
set AppleScript's text item delimiters to "TRICK"
set theList to theList as text
end repeat
set AppleScript's text item delimiters to "TRICK"
--> (theList's text 6 thru -6) = "abTRICKcd"
set theList to (theList's text 6 thru -6)'s text items
set AppleScript's text item delimiters to prevTIDs
return theList
end remove_emptyitem
--from www.macosautomation.com
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
--Search with brackets handler from Yvan Koenig
to extractBetween(SearchText, startText, endText)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to startText
set liste to text items of SearchText
set AppleScript's text item delimiters to endText
set extracts to {}
repeat with subText in liste
if subText contains endText then
copy text item 1 of subText to end of extracts
end if
end repeat
set AppleScript's text item delimiters to " "
set extracts to extracts as text
set the clipboard to extracts
set AppleScript's text item delimiters to tid
end extractBetween
-- `menu_click`, by Jacob Rus, September 2006
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment