Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fractaledmind/7441985 to your computer and use it in GitHub Desktop.

Select an option

Save fractaledmind/7441985 to your computer and use it in GitHub Desktop.
REQUIREMENTS: The Satimage osax plugin for Applescript, MultiMarkdown, and Evernote. This script accepts Markdown-styled text from the clipboard and converts it to HTML to create a new note in Evernote. This new version adds the feature of extracting [[double bracketed]] words and phrases and creating new notes for those phrases and embedding th…
property markdownloc : "/usr/local/bin/multimarkdown"
property tid : AppleScript's text item delimiters
set mdSource to the clipboard
if is_running("Evernote") = false then
tell application id "com.evernote.Evernote" to launch
tell application "System Events"
set visible of process "Evernote" to false
end tell
end if
(* ///
PART ONE: METADATA
/// *)
--check for Title metadata
try
if text item 1 of paragraph 1 of mdSource = "#" then
if not text item 2 of paragraph 1 of mdSource = "#" then
set theTitle to text items 3 thru -1 of paragraph 1 of mdSource as string
set theTitle to my trimEnd(theTitle)
set mdSource to my RemoveFromString(mdSource, paragraph 1 of mdSource)
else
set theTitle to my date_format()
set mdSource to mdSource
end if
else if paragraph 1 of mdSource contains "Title:" then
set theTitle to text items 8 thru -1 of paragraph 1 of mdSource as string
set theTitle to my trimEnd(theTitle)
set mdSource to my RemoveFromString(mdSource, paragraph 1 of mdSource)
end if
on error
set theTitle to my date_format()
set mdSource to mdSource
end try
--check of Notebook metadata
try
if text item 1 of paragraph 2 of mdSource = "=" then
set theNotebook to text items 3 thru -1 of paragraph 2 of mdSource as string
set theNotebook to my trimEnd(theNotebook)
set mdSource to my RemoveFromString(mdSource, paragraph 2 of mdSource)
else if paragraph 2 of mdSource contains "Notebook:" then
set theNotebook to text items 11 thru -1 of paragraph 2 of mdSource as string
set theNotebook to my trimEnd(theNotebook)
set mdSource to my RemoveFromString(mdSource, paragraph 2 of mdSource)
else
set theNotebook to my find_default_notebook()
set mdSource to mdSource
end if
on error
set theNotebook to my find_default_notebook()
set mdSource to mdSource
end try
--check for Tags metadata
try
if text item 1 of paragraph 3 of mdSource = "@" then
set theTags to text items 3 thru -1 of paragraph 3 of mdSource as string
set theTags to my trimEnd(theTags)
set AppleScript's text item delimiters to ", "
set theTags to text items of theTags
set AppleScript's text item delimiters to tid
set mdSource to my RemoveFromString(mdSource, paragraph 3 of mdSource)
else if paragraph 3 of mdSource contains "Keywords:" then
set theTags to text items 11 thru -1 of paragraph 3 of mdSource as string
set theTags to my trimEnd(theTags)
set AppleScript's text item delimiters to ", "
set theTags to text items of theTags
set AppleScript's text item delimiters to tid
set mdSource to my RemoveFromString(mdSource, paragraph 3 of mdSource)
else
set theTags to ""
set mdSource to mdSource
end if
on error
set theTags to ""
set mdSource to mdSource
end try
(* ///
PART TWO: AUTO-LINKING
/// *)
--find all [[double bracketed]] terms or phrases
set bracket_regex to find text "\\[\\[(.*?)\\]\\]" in mdSource with regexp and all occurrences
if not bracket_regex = {} then
--create mirror lists of [[bracketed terms]] and unbracketed terms
set terms_bracketed to {}
set terms_unbracketed to {}
repeat with i from 1 to count of bracket_regex
set x to matchResult of item i of bracket_regex
copy x to end of terms_bracketed
set term_unbracketed to text items 3 thru -3 of x as string
copy term_unbracketed to end of terms_unbracketed
end repeat
--remove any duplicates from both lists
set terms_unbracketed to my remove_duplicates(terms_unbracketed)
set terms_bracketed to my remove_duplicates(terms_bracketed)
--ensure lists are of equal length
if not (count of terms_bracketed) = (count of terms_unbracketed) then
display notification "Script Error"
else
set l to {}
set urls to {}
--work thru each bracketed term
repeat with i from 1 to count of terms_unbracketed
tell application id "com.evernote.Evernote"
--see if any pre-existing notes are entitled with the bracketed term
set matches to (find notes "intitle:\"" & item i of terms_unbracketed & "\"")
end tell
--if there are any pre-existing notes, then allow user to choose to link to one of them
if not matches = {} then
repeat with j from 1 to count of matches
set t to title of item j of matches
set n to name of notebook of item j of matches
copy t & " :: " & n to end of l
end repeat
set choice to choose from list l with title "Link to Pre-Existing Note" OK button name "Select" cancel button name "Create New"
--if user still wants to create new note
tell application id "com.evernote.Evernote"
if result of choice = false then
set Wiki_note to create note title item i of terms_unbracketed with text "" notebook theNotebook
assign "Wiki" to Wiki_note
repeat until isSynchronizing is false
end repeat
synchronize
repeat until isSynchronizing is false
end repeat
set noteURL to note link of Wiki_note
set MD_link to "[" & item i of terms_unbracketed & "](" & noteURL & ")"
copy {item i of terms_bracketed, MD_link} to end of urls
--if user wants to link to pre-existing note
else
set pos to my list_position((choice as string), l)
set noteURL to note link of item pos of matches
set noteName to title of item pos of matches
set MD_link to "[" & noteName & "](" & noteURL & ")"
copy {item i of terms_bracketed, MD_link} to end of urls
end if
--reset list
set l to {}
end tell
--if there aren't any pre-existing notes, create new note
else if matches = {} then
tell application id "com.evernote.Evernote"
set Wiki_note to create note title item i of terms_unbracketed with text "" notebook theNotebook
set wikiTag to tag "wiki"
assign wikiTag to Wiki_note
repeat until isSynchronizing is false
end repeat
synchronize
repeat until isSynchronizing is false
end repeat
set noteURL to note link of Wiki_note
set MD_link to "[" & item i of terms_unbracketed & "](" & noteURL & ")"
copy {item i of terms_bracketed, MD_link} to end of urls
end tell
end if
end repeat
end if
--replace bracketed terms with Markdown-style links
set newMD to mdSource
repeat with i from 1 to count of urls
set find to item 1 of item i of urls
set replace to item 2 of item i of urls
set newMD to change find into replace in newMD
end repeat
copy "Wiki" to end of theTags
else
set newMD to mdSource
end if
(* ///
PART THREE: MMD TO HTML CONVERSION
/// *)
--convert (Multi)Markdown to HTML
set rawHTML to do shell script "echo " & the quoted form of newMD & " | " & markdownloc
(* ///
PART FOUR: CREATE EVERNOTE NOTE
/// *)
--create new Evernote note
tell application id "com.evernote.Evernote"
if (not (notebook named theNotebook exists)) then
make notebook with properties {name:theNotebook}
end if
set n to create note title theTitle with html rawHTML notebook theNotebook
repeat with i from 1 to count of theTags
set x to item i of theTags
if (not (tag named x exists)) then
set tag1 to make tag with properties {name:x}
else
set tag1 to tag x
end if
assign tag1 to n
end repeat
--is Growl installed?
set Growl_ to my Growl_Check()
--notify on Success
if Growl_ is true then
tell application "Growl"
set allNotificationsFiles to {"Success Notification", "Failure Notification"}
set enabledNotificationsFiles to {"Success Notification", "Failure Notification"}
register as application ¬
"Skim Annotations to Evernote" all notifications allNotificationsFiles ¬
default notifications enabledNotificationsFiles ¬
icon of application "URL Handler"
end tell
--on Success
if n exists then
tell application "Growl"
notify with name ¬
"Success Notification" title ¬
"Created note: " & theTitle description "Notebook: " & theNotebook application name "Markdown2Evernote"
end tell
end if
end if
end tell
(* HANDLERS *)
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
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
to date_format() -- Old_date is text, not a date.
set {year:y, month:m, day:d} to (current date)
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8
end date_format
on find_default_notebook()
tell application id "com.evernote.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 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
return lst as text
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't RemoveFromString: " & eMsg number eNum
end try
end RemoveFromString
--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
on list_position(this_item, this_list)
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then return i
end repeat
return 0
end list_position
--Check For Growl
on Growl_Check()
if appIsRunning("Growl") then
set growl_Running to true
tell application "Growl"
set allNotificationsFiles to {"Markdown To Evernote", "Success Notification", "Failure Notification"}
set enabledNotificationsFiles to {"Markdown To Evernote", "Success Notification", "Failure Notification"}
register as application ¬
"Markdown2Evernote" all notifications allNotificationsFiles ¬
default notifications enabledNotificationsFiles
end tell
return growl_Running
else
error
end if
end Growl_Check
--App Detect
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment