Skip to content

Instantly share code, notes, and snippets.

@vladkosinov
Created December 17, 2021 11:46
Show Gist options
  • Save vladkosinov/1f0771dc66c8ff7b09a9f7d80d083065 to your computer and use it in GitHub Desktop.
Save vladkosinov/1f0771dc66c8ff7b09a9f7d80d083065 to your computer and use it in GitHub Desktop.
export apple script notes to html
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 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
on yyyymmddhhmmss(date_)
set y to year of date_
set m_ to month of (date_) as integer
if m_ < 10 then
set m to "0" & (m_ as string)
else
set m to (m_ as string)
end if
set dnum to (day of (date_)) as integer
if dnum < 10 then
set d to "0" & (dnum as string)
else
set d to (dnum as string)
end if
set t to time of date_
set h_ to (round (t / 3600) rounding down)
if h_ < 10 then
set h to "0" & (h_ as string)
else
set h to (h_ as string)
end if
set t to t - h_ * 3600
set min_ to (round (t / 60) rounding down)
if min_ < 10 then
set minstr to "0" & (min_ as string)
else
set minstr to (min_ as string)
end if
set t to t - min_ * 60
if t < 10 then
set s to "0" & (t as string)
else
set s to (t as string)
end if
set result to (y & "-" & m & "-" & d & "-" & h & "-" & minstr & "-" & s)
return result
end yyyymmddhhmmss
tell application "Notes"
activate
display dialog "This is the export utility for Notes.app.
" & "Exactly " & (count of notes) & " notes are stored in the application. " & "Each one of them will be exported as a simple HTML file stored in a folder of your choice." with title "Notes Export" buttons {"Cancel", "Proceed"} cancel button "Cancel" default button "Proceed"
set exportFolder to choose folder
repeat with each in every note
set noteName to name of each
set noteBody to body of each as text
set itemProps to properties of each
log itemProps
set d to creation date of each
set timef to my yyyymmddhhmmss(d)
set noteName to my replace_chars(noteName, ":", "-")
set filename to ((exportFolder as string) & timef & "-" & noteName & ".html")
my writeToFile(filename, noteBody as text)
end repeat
display alert "Notes Export" message "All notes were exported successfully." as informational
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment