Skip to content

Instantly share code, notes, and snippets.

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 glowinthedark/5593841b92f5bd1334dc26d01fc6af46 to your computer and use it in GitHub Desktop.
Save glowinthedark/5593841b92f5bd1334dc26d01fc6af46 to your computer and use it in GitHub Desktop.
Export notes from the OSX Notes.app to separate HTML files prefixed with the creation timestamp.
on buildTitle(originalText)
set normalizedText to my replace(originalText, ":", "-")
set normalizedText to my replace(normalizedText, "|", "")
set normalizedText to my replace(normalizedText, "{", "")
set normalizedText to my replace(normalizedText, "}", "")
set finalTitle to my firstChars(normalizedText, 30)
return finalTitle
end buildTitle
on replace(originalText, fromText, toText)
set AppleScript's text item delimiters to the fromText
set the item_list to every text item of originalText
set AppleScript's text item delimiters to the toText
set originalText to the item_list as string
set AppleScript's text item delimiters to ""
return originalText
end replace
on firstChars(originalText, maxChars)
if length of originalText is less than maxChars then
return originalText
else
set limitedText to text 1 thru maxChars of originalText
return limitedText
end if
end firstChars
on writeToFile(filename, filecontents)
set the output to open for access file filename with write permission
set eof of the output to 0
write filecontents to the output starting at eof as «class utf8»
close access the output
end writeToFile
on dateTimeToIso(dt)
set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to dt
set y to text 2 through -1 of ((y + 10000) as text)
set m to text 2 through -1 of ((m + 100) as text)
set d to text 2 through -1 of ((d + 100) as text)
set h to text 2 through -1 of ((h + 100) as text)
set min to text 2 through -1 of ((min + 100) as text)
set s to text 2 through -1 of ((s + 100) as text)
return y & "-" & m & "-" & d & "_" & h & "_" & min
end dateTimeToIso
tell application "Notes"
activate
set exportFolder to POSIX path of (choose folder with prompt "Please choose the export folder") as string
set exportFolder to (POSIX file exportFolder) as string
set counter to 0
repeat with each in every note
set noteName to name of each
set noteBody to body of each
set noteTitle to my buildTitle(noteName)
set creationDate to creation date of each
set timeStamp to my dateTimeToIso(creationDate) as string
set counter to counter + 1
set filename to ((exportFolder as string) & timeStamp & "_" & noteTitle & ".html")
my writeToFile(filename, noteBody as text)
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment