Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created October 11, 2013 17:23
Show Gist options
  • Select an option

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

Select an option

Save fractaledmind/6938632 to your computer and use it in GitHub Desktop.
This Applescript will take .xml data files from the Dickinson College Commentaries Greek and Latin Core Vocabulary lists and import each term into Evernote for use with Evernote Peek. Each term becomes a single note. The script will, if necessary, create new notebooks ("Greek Vocab" or "Latin Vocab", depending upon which list you are importing).…
--27.5 minutes to upload to Evernote the entire Latin Core Vocab list (997 terms)
--7.5 minutes to upload to Evernote the entire Greek Core Vocab list (515 terms)
property tid : AppleScript's text item delimiters
property alphaList : "abcdefghijklmnopqrstuvwxyz"'s items & reverse of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"'s items
--get xml file from clipboard and clean up
set t to my RemoveListFromString(the clipboard, {"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", "<nodes>", "</nodes>"})
--remove tabbed whitespace from list
set ps to paragraphs of t
set li to {}
repeat with i from 1 to count of ps
set x to item i of ps
set x to trimBoth(x)
copy x to end of li
end repeat
--group list by nodes
set x to my list_positions(li, "<node>", true)
set y to my list_positions(li, "</node>", true)
set l to my interlaceLists(x, y)
set lx to my groupList(l, 2)
--create full list of all data with individual nodes as sub-lists
set dataList to {}
repeat with i from 1 to count of lx
set z to item i of lx
set thefront to item 1 of z
set theback to item 2 of z
set myfront to (thefront + 1)
set myback to (theback - 1)
set thisItem to items myfront through myback of li
copy thisItem to end of dataList
end repeat
--parse individual items and export to Evernote
set thebody to ""
repeat with i in dataList
set theItem to i
repeat with j in theItem
if j contains "<Headword>" then
set AppleScript's text item delimiters to {"<Headword>", "</Headword>"}
set thetitle to text item 2 of j
--this is the XML coding for the Latin
else if j contains "<Definitions>" then
set theLanguage to "Latin"
set AppleScript's text item delimiters to {"<Definitions>", "</Definitions>"}
set theDef to text item 2 of j
set thebody to thebody & "<p><strong>Definitions:</strong> " & theDef & " / </p>"
--this is the XML coding for the Greek
else if j contains "<Definition>" then
set theLanguage to "Greek"
set AppleScript's text item delimiters to {"<Definition>", "</Definition>"}
set theDef to text item 2 of j
set thebody to thebody & "<p><strong>Definitions:</strong> " & theDef & " / </p>"
else if j contains "<Part-of-Speech>" then
set AppleScript's text item delimiters to {"<Part-of-Speech>", "</Part-of-Speech>"}
set thePoS to text item 2 of j
set thePoS to my changeCase(thePoS, "title")
if thePoS contains "And" then
set thePoS to my replace_chars(thePoS, "And", "and")
else if thePoS contains "," then
set thePoS to my replace_chars(thePoS, ",", " ")
end if
else if j contains "<Semantic-Group>" then
set AppleScript's text item delimiters to {"<Semantic-Group>", "</Semantic-Group>"}
set theSemantic to text item 2 of j
if theSemantic contains "," then
set theSemantic to my replace_chars(theSemantic, ",", ":")
set theSemantic to my trimBoth(theSemantic)
end if
else if j contains "<Frequency-Rank>" then
set AppleScript's text item delimiters to {"<Frequency-Rank>", "</Frequency-Rank>"}
set theFrequency to text item 2 of j
set thebody to thebody & "<p><strong>Frequency Rank:</strong> " & theFrequency & " / </p>"
end if
end repeat
tell application "Evernote"
set NBname to theLanguage & " Vocab"
if (not (notebook named NBname exists)) then
set theNB to make notebook with properties {name:NBname}
else
set theNB to theLanguage & " Vocab"
end if
if (not (tag named thePoS exists)) then
set synTag to make tag with properties {name:thePoS}
else
set synTag to tag named thePoS
end if
if (not (tag named theSemantic exists)) then
set semTag to make tag with properties {name:theSemantic}
else
set semTag to tag named theSemantic
end if
set theNote to create note title thetitle with html thebody notebook theNB
assign semTag to theNote
assign synTag to theNote
end tell
set thebody to ""
end repeat
(* HANDLERS *)
on RemoveListFromString(theText, listOfCharsOrStrings)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, theText, CharOrString, lst
try
script k
property l : listOfCharsOrStrings
end script
set len to count k's l
repeat with i from 1 to len
set cur_ to k's l's item i
set theText to my RemoveFromString(theText, cur_)
end repeat
return theText
on error eMsg number eNum
error "Can't RemoveListFromString: " & eMsg number eNum
end try
end RemoveListFromString
on trimBoth(str)
-- HAS (http://applemods.sourceforge.net/mods/Data/String.php)
local str
try
return my trimStart(my trimEnd(str))
on error eMsg number eNum
error "Can't trimBoth: " & eMsg number eNum
end try
end trimBoth
on list_positions(this_list, this_item, list_all)
set the offset_list to {}
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then
set the end of the offset_list to i
if list_all is false then return item 1 of offset_list
end if
end repeat
if list_all is false and offset_list is {} then return 0
return the offset_list
end list_positions
on interlaceLists(list1, list2)
-- HAS (http://applemods.sourceforge.net/mods/Data/List.php)
local list1, list2
try
if list1's class is not list then error "not a list." number -1704
if list2's class is not list then error "not a list." number -1704
script k
property l1 : list1
property l2 : list2
property res : {}
end script
if (count of k's l1) is not (count of k's l2) then error "lists are different lengths."
repeat with i from 1 to count k's l1
set k's res's end to k's l1's item i
set k's res's end to k's l2's item i
end repeat
return k's res
on error eMsg number eNum
error "Can't interlaceLists: " & eMsg number eNum
end try
end interlaceLists
on groupList(lst, groupLen)
-- HAS (http://applemods.sourceforge.net/mods/Data/List.php)
local lst, tailLen, groupLen, idx
try
if lst's class is not list then error "not a list." number -1704
script k
property l : lst
property res : {}
end script
set tailLen to (count of k's l) mod groupLen
repeat with idx from 1 to ((count of k's l) - tailLen) by groupLen
set k's res's end to k's l's items idx thru (idx + groupLen - 1)
end repeat
if tailLen is not 0 then
set k's res's end to k's l's items -tailLen thru -1
end if
return k's res
on error eMsg number eNum
error "Can't groupList: " & eMsg number eNum
end try
end groupList
on changeCase(t, c)
if (count t) is 0 then return t
considering case
if c is not in {"upper", "lower", "title", "sentence"} then
error "The word \"" & c & "\" is not a valid option. Please use \"upper\", \"lower\", \"title\" or \"sentence\"."
else if c is "upper" then
set n to 1
else
set n to -1
end if
set d to text item delimiters
repeat with n from n to n * 26 by n
set text item delimiters to my alphaList's item n
set t to textItems from t
set text item delimiters to my alphaList's item -n
tell t to set t to beginning & ({""} & rest)
end repeat
if c is in {"title", "sentence"} then
if c is "title" then
set s to space
else
set s to ". "
end if
set t to (t's item 1 & s & t)'s text 2 thru -1
repeat with i in {s, tab, return, ASCII character 10}
set text item delimiters to i
if (count t's text items) > 1 then repeat with n from 1 to 26
set text item delimiters to i & my alphaList's item n
if (count t's text items) > 1 then
set t to textItems from t
set text item delimiters to i & my alphaList's item -n
tell t to set t to beginning & ({""} & rest)
end if
end repeat
end repeat
set t to t's text ((count s) + 1) thru -1
end if
set text item delimiters to d
end considering
t
end changeCase
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
(* SUB-ROUTINES *)
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
on trimStart(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 first character is in whiteSpace
set str to str's text 2 thru -1
end repeat
return str
on error number -1728
return ""
end try
on error eMsg number eNum
error "Can't trimStart: " & eMsg number eNum
end try
end trimStart
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
on textItems from t
try
t's text items
on error number -2706
tell (count t's text items) div 2 to my (textItems from (t's text 1 thru text item it)) & my (textItems from (t's text from text item (it + 1) to -1))
end try
end textItems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment