Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created October 20, 2013 04:46
Show Gist options
  • Select an option

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

Select an option

Save fractaledmind/7065165 to your computer and use it in GitHub Desktop.
This script will convert previously created Evernote notes with Skim annotations that had the old "customurl" scheme into my new, iOS friendly "pdfpen" urls.
property tid : AppleScript's text item delimiters
property LF : (ASCII character 32) & (ASCII character 32) & (ASCII character 10)
tell application id "com.evernote.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 noteName to title of item i of Evernote_Selection
set noteNotebook to name of notebook of item i of Evernote_Selection
set theTags to tags of item i of Evernote_Selection
set noteTags_list to {}
repeat with j from 1 to count of theTags
set tagName to name of item j of theTags
copy tagName to the end of noteTags_list
end repeat
set newTag to "PDFpen"
copy newTag to end of noteTags_list
set noteHTML to HTML content of item i of Evernote_Selection
--find all hyperlinks
set linkNum to my findAll(noteHTML, "</a>")
set linkNum to count of linkNum
--replace all hyperlinks with PDFPen urls
set newHTML to noteHTML
repeat linkNum times
set link to my stringBetween(noteHTML, "<a href=", "</a>")
set AppleScript's text item delimiters to "/"
set theTitle to text item -1 of link
set theTitle to my replaceString(theTitle, "#", "?pg=")
set newLink to "pdfpen:///" & theTitle
set AppleScript's text item delimiters to tid
set newHTML to my replaceString(newHTML, link, newLink)
end repeat
set metadata to "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">"
set newHTML to my replaceString(newHTML, metadata, "")
--create new note with new hyperlinks
set title_list to title of every note of notebook noteNotebook
set num to my list_positions(title_list, noteName, true)
if (count of num) = 1 then
set num to num as number
else
error
end if
delete note num of notebook noteNotebook
set N to create note title noteName with html newHTML notebook noteNotebook
repeat with i from 1 to count of noteTags_list
set x to item i of noteTags_list
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
end repeat
end tell
(* HANDLERS *)
to joinList(aList, delimiter)
set retVal to ""
set prevDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set retVal to aList as string
set AppleScript's text item delimiters to prevDelimiter
return retVal
end joinList
on replaceString(theText, oldString, newString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replaceString
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 stringBetween(str, head, tail)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local str, head, tail
try
if str does not contain head then return ""
if str does not contain tail then return ""
set str to item 2 of my explode(head, str)
set str to item 1 of my explode(tail, str)
return str
on error eMsg number eNum
error "Can't stringBetween: " & eMsg number eNum
end try
end stringBetween
on explode(delimiter, input)
-- ljr (http://applescript.bratis-lover.net/library/string/),
local delimiter, input, ASTID
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to delimiter
set input to text items of input
set AppleScript's text item delimiters to ASTID
return input --> list
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't explode: " & eMsg number eNum
end try
end explode
on findAll(str, findString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, str, findString, tmp, len, len2, pos, res
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to findString
if str does not contain findString then return {}
considering case
script k
property res : {}
property tmp : str's text items
end script
end considering
set len to count k's tmp
set len2 to count findString
set pos to 0
repeat with i from 1 to len - 1
set thisPos to (count k's tmp's item i)
set thisPos to thisPos + pos
set pos to thisPos + len2
set end of k's res to (thisPos + 1)
end repeat
set AppleScript's text item delimiters to ASTID
return k's res
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't findAll: " & 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