Skip to content

Instantly share code, notes, and snippets.

@DrLulz
Created December 12, 2014 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrLulz/e55470b51025870c32e7 to your computer and use it in GitHub Desktop.
Save DrLulz/e55470b51025870c32e7 to your computer and use it in GitHub Desktop.
Export Skim annotations to Evernote. Requires Hackademic's "Skimmer URL Handler" (https://gist.github.com/smargh/10218589)
property line_feed : (ASCII character 10)
property md_line_feed : (ASCII character 32) & (ASCII character 32) & (ASCII character 10)
property as_delims : AppleScript's text item delimiters
property export_style : "HTML"
property export_destination : "Evernote"
--Text Note HTML
property text_prefix : "<p style=\"font-size: 14px;\">"
property text_body_wrap_front : "<span style=\"font-family: 'Helvetica Neue';\">"
property text_body_wrap_back : "</span>"
property text_page_wrap_front : " (<a href=\""
property text_page_abbr : "\">p."
property text_page_wrap_back : "</a>)"
--Anchor Note HTML
property anchored_prefix : "<p style=\"font-size: 14px;\">"
property anchored_title_wrap_front : "<strong>"
property anchored_title_wrap_back : "</strong>"
property anchored_body_wrap_front : "<span style=\"font-family: 'Helvetica Neue';\">"
property anchored_body_wrap_back : "</span>"
property anchored_page_wrap_front : "(<a href=\""
property anchored_page_abbr : "\"><font color=\"#4C4C4C\">p."
property anchored_page_wrap_back : "</font></a>)"
--Underline Note HTML
property underline_prefix : "<p style=\"font-size: 14px;\">"
property underline_body_wrap_front : "\""
property underline_body_wrap_back : "\""
property underline_page_wrap_front : "(<a href=\""
property underline_page_abbr : "\">p."
property underline_page_wrap_back : "</a>)"
--Strike-Thru Note HTML
property strike_prefix : "<p style=\"font-size: 14px;\">"
property strike_body_wrap_front : "\""
property strike_body_wrap_back : "\""
property strike_page_wrap_front : "(<a href=\""
property strike_page_abbr : "\">p."
property strike_page_wrap_back : "</a>)"
---------------------------------------------------
--HIGHLIGHTS
---------------------------------------------------
--Highlight Note HTML FAV1
property one_highlight_space : "<p style=\"font-size: 10px;\"><hr/><p style=\"font-size: 10px;\"></p>"
property one_highlight_prefix : "<p style=\"font-size: 18px;\">"
property one_highlight_title_wrap_front : "<a href=\""
property one_highlight_title_wrap_mid : "\" style=\"text-decoration: none\">"
property one_highlight_title_wrap_back : "</a>"
property one_highlight_body_wrap_front : "<b><span style=\"font-family: 'Helvetica Neue';\">"
property one_highlight_body_wrap_back : "</span></b>"
property one_highlight_page_wrap_front : ""
property one_highlight_page_abbr : "<font size=\"1\" color=\"#4C4C4C\">("
property one_highlight_page_wrap_back : ")</font>"
--Highlight Note HTML FAV2
property two_highlight_prefix : "<p style=\"font-size: 16px;\">"
property two_highlight_title_wrap_front : "<a href=\""
property two_highlight_title_wrap_mid : "\" style=\"text-decoration: none\">"
property two_highlight_title_wrap_back : "</a>"
property two_highlight_body_wrap_front : "<b><span style=\"font-family: 'Helvetica Neue';\">"
property two_highlight_body_wrap_back : "</span></b>"
property two_highlight_page_wrap_front : ""
property two_highlight_page_abbr : "<font size=\"1\" color=\"#4C4C4C\">("
property two_highlight_page_wrap_back : ")</font>"
--Highlight Note HTML FAV3
property three_highlight_prefix : "<p style=\"font-size: 16px;\">"
property three_highlight_title_wrap_front : "<a href=\""
property three_highlight_title_wrap_mid : "\" style=\"text-decoration: none\">"
property three_highlight_title_wrap_back : "</a>"
property three_highlight_body_wrap_front : "<b><span style=\"font-family: 'Helvetica Neue';\">"
property three_highlight_body_wrap_back : "</span></b>"
property three_highlight_page_wrap_front : ""
property three_highlight_page_abbr : "<font size=\"1\" color=\"#4C4C4C\">("
property three_highlight_page_wrap_back : ")</font>"
--Highlight Note HTML FAV4
property four_highlight_prefix : "<p style=\"font-size: 16px;\">"
property four_highlight_title_wrap_front : "<strong>"
property four_highlight_title_wrap_back : "</strong>"
property four_highlight_body_wrap_front : "<span style=\"font-family: 'Helvetica Neue';\">"
property four_highlight_body_wrap_back : "</span>"
property four_highlight_page_wrap_front : ""
property four_highlight_page_abbr : ""
property four_highlight_page_wrap_back : ""
--Highlight Note HTML FAV5
property five_highlight_prefix : "<p style=\"font-size: 16px;\">"
property five_highlight_title_wrap_front : "<a href=\""
property five_highlight_title_wrap_mid : "\" style=\"text-decoration: none\">"
property five_highlight_title_wrap_back : "</a>"
property five_highlight_body_wrap_front : "<span style=\"color:#CC7A29\" style=\"font-family: 'Helvetica Neue';\">"
property five_highlight_body_wrap_back : "</span>"
property five_highlight_page_wrap_front : ""
property five_highlight_page_abbr : "<font size=\"1\" color=\"#4C4C4C\">("
property five_highlight_page_wrap_back : ")</font>"
--Highlight Note HTML FAV6
property six_highlight_prefix : "<p style=\"font-size: 16px;\">"
property six_highlight_title_wrap_front : "<a href=\""
property six_highlight_title_wrap_mid : "\" style=\"text-decoration: none\">"
property six_highlight_title_wrap_back : "</a>"
property six_highlight_body_wrap_front : "<span style=\"color:#B00000\" style=\"font-family: 'Helvetica Neue';\">"
property six_highlight_body_wrap_back : "</span>"
property six_highlight_page_wrap_front : ""
property six_highlight_page_abbr : "<font size=\"1\" color=\"#4C4C4C\">("
property six_highlight_page_wrap_back : ")</font>"
--Highlight Note HTML
--property highlight_prefix : "<h2>"
--property highlight_title_wrap_front : "<strong>"
--property highlight_title_wrap_back : ":</strong>"
--property highlight_body_wrap_front : ""
--property highlight_body_wrap_back : ""
--property highlight_page_wrap_front : "(<a href=\""
--property highlight_page_abbr : "\">p."
--property highlight_page_wrap_back : "</a>)"
on run
--Get path to current directory
set base_path to my get_base_path()
tell application "Skim"
try
if every note of front document = {} then convert notes front document
save front document
on error msg
return msg & " in Pre-Processing."
end try
set pdf_name to (name of front document)
set _file to (path of front document)
set file_url to my encode_text(_file, false, false)
set skimmer_url to "skimmer://" & file_url & "?page="
set all_notes to every note of front document
set notes_text to my get_header(export_style, "Text Notes")
set notes_anchor to my get_header(export_style, "Topics")
set notes_underline to my get_header(export_style, "Underlined")
set notes_strikethru to my get_header(export_style, "Strike-Through")
set notes_highlight to my get_header(export_style, "Outline")
set {_text_, _anchor_, _underline_, _strikethru_, _highlight_} to {false, false, false, false, false}
repeat with i from 1 to count of all_notes
set _note to item i of all_notes
set _page to index of page of _note
set real_page to _page as string
set this_url to skimmer_url & _page
if type of _note is text note then
set note_text to text of _note
set notes_text to notes_text & (my get_annotation_hyperlink((type of _note), "", note_text, this_url, real_page))
set _text_ to true
else if type of _note is anchored note then
set title_text to text of _note
set note_text to extended text of _note
set notes_anchor to notes_anchor & (my get_annotation_hyperlink((type of _note), title_text, note_text, this_url, real_page))
set _anchor_ to true
else if type of _note is underline note then
set note_text to text of _note
set notes_underline to notes_underline & (my get_annotation_hyperlink((type of _note), "", note_text, this_url, real_page))
set _underline_ to true
else if type of _note is strike out note then
set note_text to text of _note
set notes_strikethru to notes_strikethru & (my get_annotation_hyperlink((type of _note), "", note_text, this_url, real_page))
set _strikethru_ to true
else if type of _note is highlight note then
set note_text to text of _note
set rgba to color of _note
set rgb to items 1 thru 3 of rgba
set hex to my RBG_to_HTML(rgb)
set fave_colors to favorite colors
set title_text to my color2text(rgba, fave_colors)
set notes_highlight to my notes_highlight & (my get_annotation_highlight(rgba, fave_colors, title_text, note_text, this_url, real_page, hex))
set _highlight_ to true
end if
end repeat
--Remove any Empty Annotation Sections
set final_text to ""
if _text_ = true then set final_text to final_text & notes_text
if _anchor_ = true then set final_text to final_text & notes_anchor
if _underline_ = true then set final_text to final_text & notes_underline
if _strikethru_ = true then set final_text to final_text & notes_strikethru
if _highlight_ = true then set final_text to final_text & notes_highlight
--Export the Notes
if export_destination = "Evernote" then
set _evernote_ to false
repeat until _evernote_ = true
tell application "System Events"
if not (exists process "Evernote") then
tell application id "com.evernote.Evernote" to activate
delay 1
tell application id "com.evernote.Evernote" to activate
end if
if (exists process "Evernote") then set _evernote_ to true
end tell
end repeat
tell application id "com.evernote.Evernote"
activate
--Choose/Create Notebook for Export
set allNotebooks to the name of every notebook
set chosenNotebook to choose from list my sort(allNotebooks) with prompt "Choose Notebook:" cancel button name "New Notebook"
if chosenNotebook is not false then
set en_notebook to the result
else if chosenNotebook is false then
set defaultTag to "NEW NOTEBOOK"
set DialogTitle to "Create New Notebook"
display dialog "Enter the Name of the New Notebook" with title DialogTitle default answer defaultTag buttons {"Create", "Cancel"} default button "Create" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
set newNotebook to the result
set en_notebook to text returned of newNotebook
set ButtonSel to button returned of newNotebook
if ButtonSel is "Create" then
make notebook with properties {name:en_notebook}
end if
end if
if export_style = "HTML" then
set newNote to create note title pdf_name with html final_text notebook en_notebook
else if export_style = "Markdown" then
set newNote to create note title pdf_name with text final_text notebook en_notebook
end if
--Choose/Create Tags for Export
set allTags to the name of every tag
set chosenTags to choose from list my sort(allTags) with prompt "Choose Tags:" cancel button name "New Tag" with multiple selections allowed
if chosenTags is not false then
set theDelims to {", ", ","}
set EVTag to my Tag_List(chosenTags, theDelims)
set final_Tag_List to my Tag_Check(EVTag)
assign final_Tag_List to newNote
else if chosenTags is false then
set defaultTag to "ENTER YOUR TAGS HERE"
set DialogTitle to "Assign Tags On Exported Skim Notes"
display dialog "Enter Your Tags Separated By Commas" with title DialogTitle default answer defaultTag buttons {"Assign", "Cancel"} default button "Assign" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
set dialogresult to the result
set userInput to text returned of dialogresult
set ButtonSel to button returned of dialogresult
set theDelims to {", ", ","}
set EVTag to my Tag_List(userInput, theDelims)
set final_Tag_List to my Tag_Check(EVTag)
if ButtonSel is "Assign" then
--tell application "Evernote"
assign final_Tag_List to newNote
--end tell
end if
end if
--synchronize Evernote
repeat until isSynchronizing is false
synchronize
end repeat
repeat until isSynchronizing is false
end repeat
end tell
else if export_destination = "Clipboard" then
set the clipboard to final_text
end if
return "Exported notes to " & export_destination & " as " & export_style
end tell
end run
(* HANDLERS *)
on get_base_path()
set {as_delims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set _path to (text items 1 thru -2 of (POSIX path of (path to me)) as string) & "/"
set AppleScript's text item delimiters to as_delims
return _path
end get_base_path
on get_header(_style, _header)
if _style = "HTML" then
--return "<hr />" & line_feed & line_feed & "<h2>" & _header & "</h2>" & md_line_feed & line_feed & line_feed
return line_feed & line_feed & md_line_feed & line_feed & line_feed
else if _style = "Markdown" then
return "- - -" & line_feed & line_feed & "## " & _header & " ##" & md_line_feed & line_feed & line_feed
end if
end get_header
on get_annotation_hyperlink(_type, _title, note_text, hyperlink, real_page)
--For the formulae below, properties are wrapped in {curlies} and passed parameters are wrapped in <carets>. Also, note where the spaces are.
tell application "Skim"
if _type = text note then
--{prefix}{wrap}<note text>{/wrap}{wrap}<link>{p.} <#>{/wrap}
set body to (text_prefix & text_body_wrap_front & note_text & text_body_wrap_back)
set page_front to (text_page_wrap_front & hyperlink & text_page_abbr)
set page_back to (real_page & text_page_wrap_back)
return body & space & page_front & space & page_back & line_feed & line_feed
else if _type = anchored note then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set anchor to (anchored_prefix & anchored_title_wrap_front & _title & anchored_title_wrap_back)
set body to (anchored_body_wrap_front & note_text & anchored_body_wrap_back)
set page_front to (anchored_page_wrap_front & hyperlink & anchored_page_abbr)
set page_back to (real_page & anchored_page_wrap_back)
return anchor & space & body & space & page_front & space & page_back & line_feed & line_feed
else if _type = underline note then
--{prefix}{wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set body to (underline_prefix & underline_body_wrap_front & note_text & underline_body_wrap_back)
set page_front to (underline_page_wrap_front & hyperlink & underline_page_abbr)
set page_back to (real_page & underline_page_wrap_back)
return body & space & page_front & space & page_back & line_feed & line_feed
else if _type = strike out note then
--{prefix}{wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set body to (strike_prefix & strike_body_wrap_front & note_text & strike_body_wrap_back)
set page_front to (strike_page_wrap_front & hyperlink & strike_page_abbr)
set page_back to (real_page & strike_page_wrap_back)
return body & space & page_front & space & page_back & line_feed & line_feed
end if
end tell
end get_annotation_hyperlink
on get_annotation_highlight(rgba, fave_colors, _title, note_text, hyperlink, real_page, hex)
set {fav1, fav2, fav3, fav4, fav5, fav6} to fave_colors
if rgba is equal to fav1 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set separation to (one_highlight_space)
set header to (one_highlight_prefix & one_highlight_title_wrap_front & hyperlink & one_highlight_title_wrap_mid & _title & one_highlight_title_wrap_back)
set body to (one_highlight_body_wrap_front & note_text & one_highlight_body_wrap_back)
set page_front to (one_highlight_page_wrap_front & one_highlight_page_abbr)
set page_back to (real_page & one_highlight_page_wrap_back)
return separation & space & header & space & body & space & page_front & page_back & line_feed & line_feed
else if rgba is equal to fav2 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set header to (two_highlight_prefix & two_highlight_title_wrap_front & hyperlink & two_highlight_title_wrap_mid & _title & two_highlight_title_wrap_back)
set body to (two_highlight_body_wrap_front & note_text & two_highlight_body_wrap_back)
set page_front to (two_highlight_page_wrap_front & two_highlight_page_abbr)
set page_back to (real_page & two_highlight_page_wrap_back)
return header & space & body & space & page_front & page_back & line_feed & line_feed
else if rgba is equal to fav3 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set header to (three_highlight_prefix & three_highlight_title_wrap_front & hyperlink & three_highlight_title_wrap_mid & _title & three_highlight_title_wrap_back)
set body to (three_highlight_body_wrap_front & note_text & three_highlight_body_wrap_back)
set page_front to (three_highlight_page_wrap_front & three_highlight_page_abbr)
set page_back to (real_page & three_highlight_page_wrap_back)
return header & space & body & space & page_front & page_back & line_feed & line_feed
else if rgba is equal to fav4 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set header to (four_highlight_prefix & four_highlight_title_wrap_front & _title & four_highlight_title_wrap_back)
set body to (four_highlight_body_wrap_front & note_text & four_highlight_body_wrap_back)
set page_front to (four_highlight_page_wrap_front & four_highlight_page_abbr)
set page_back to (four_highlight_page_wrap_back)
return header & space & body & space & page_front & page_back & line_feed & line_feed
else if rgba is equal to fav5 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set header to (five_highlight_prefix & five_highlight_title_wrap_front & hyperlink & five_highlight_title_wrap_mid & _title & five_highlight_title_wrap_back)
set body to (five_highlight_body_wrap_front & note_text & five_highlight_body_wrap_back)
set page_front to (five_highlight_page_wrap_front & five_highlight_page_abbr)
set page_back to (real_page & five_highlight_page_wrap_back)
return header & space & body & space & page_front & page_back & line_feed & line_feed
else if rgba is equal to fav6 then
--{prefix}{wrap}<title>{/wrap} {wrap}<note text>{/wrap} {wrap}<link>{p.} <#>{/wrap}
set header to (six_highlight_prefix & six_highlight_title_wrap_front & hyperlink & six_highlight_title_wrap_mid & _title & six_highlight_title_wrap_back)
set body to (six_highlight_body_wrap_front & note_text & six_highlight_body_wrap_back)
set page_front to (six_highlight_page_wrap_front & six_highlight_page_abbr)
set page_back to (real_page & six_highlight_page_wrap_back)
return header & space & body & space & page_front & page_back & line_feed & line_feed
end if
end get_annotation_highlight
on color2text(rgba, fave_colors)
set colorText to ""
set html_tab to "&nbsp;"
if rgba is (item 1 of fave_colors) then
set colorText to "⬛️" & html_tab
else if rgba is (item 2 of fave_colors) then
set colorText to my str_multiply(4, html_tab) & "▪️"
else if rgba is (item 3 of fave_colors) then
set colorText to my str_multiply(7, html_tab) & "▫️"
else if rgba is (item 4 of fave_colors) then
set colorText to my str_multiply(16, html_tab)
else if rgba is (item 5 of fave_colors) then
set colorText to str_multiply(11, html_tab) & "📎"
else if rgba is (item 6 of fave_colors) then
set colorText to str_multiply(11, html_tab) & "❗️"
end if
return colorText
end color2text
on str_multiply(x, str)
set new to str
repeat x times
set new to new & str
end repeat
return new
end str_multiply
on RBG_to_HTML(RGB_values)
-- NOTE: this sub-routine expects the RBG values to be from 0 to 65535
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set the the hex_value to ""
repeat with i from 1 to the count of the RGB_values
set this_value to (item i of the RGB_values) div 256
if this_value is 256 then set this_value to 255
set x to item ((this_value div 16) + 1) of the hex_list
set y to item (((this_value / 16 mod 1) * 16) + 1) of the hex_list
set the hex_value to (the hex_value & x & y) as string
end repeat
return ("#" & the hex_value) as string
end RBG_to_HTML
--URL encode text
on encode_text(this_text, encode_URL_A, encode_URL_B)
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
set the URL_B_chars to ".-_:"
set the acceptable_characters to the standard_characters
if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
set the encoded_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the encoded_text to (the encoded_text & this_char)
else
set the encoded_text to (the encoded_text & encode_char(this_char)) as string
end if
end repeat
return the encoded_text
end encode_text
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char
--CREATES TAGS LIST
on Tag_List(userInput, theDelims)
set oldDelims to AppleScript's text item delimiters
set theList to {userInput}
repeat with aDelim in theDelims
set AppleScript's text item delimiters to aDelim
set newList to {}
repeat with anItem in theList
set newList to newList & text items of anItem
end repeat
set theList to newList
end repeat
return theList as list
set AppleScript's text item delimiters to oldDelims
end Tag_List
--CREATES TAGS IF THEY DON'T EXIST
on Tag_Check(theTags)
tell application "Evernote"
set finalTags to {}
repeat with theTag in theTags
if (not (tag named theTag exists)) then
set makeTag to make tag with properties {name:theTag}
set end of finalTags to makeTag
else
set end of finalTags to tag theTag
end if
end repeat
end tell
return finalTags
end Tag_Check
--Alphabetize Notebooks & Tags
on sort(aList)
set indexList to {}
set sortedList to {}
repeat (the number of items in aList) times
set lastItem to ""
repeat with i from 1 to (number of items in aList)
if i is not in the indexList then
set aItem to item i of aList
if lastItem is "" then
set lastItem to aItem
set lastItemIndex to i
else if aItem comes before lastItem then
set lastItem to aItem
set lastItemIndex to i
end if
end if
end repeat
set end of sortedList to lastItem
set end of indexList to lastItemIndex
end repeat
return sortedList
end sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment