Skip to content

Instantly share code, notes, and snippets.

@alexandergottlieb
Last active March 11, 2022 16:30
Show Gist options
  • Save alexandergottlieb/f4b71ecf8919a4278ff5101c7715debc to your computer and use it in GitHub Desktop.
Save alexandergottlieb/f4b71ecf8919a4278ff5101c7715debc to your computer and use it in GitHub Desktop.
Export Apple Notes to HTML, one folder at a time. Uses Bear's export script but with the ability to retain your folder structure rather than dumping all notes into one folder - https://bear.app/faq/Import%20&%20export/Migrate%20from%20Apple%20Notes/
set requestedFolder to text returned of (display dialog "Which folder of Notes would you like to export? (the name must match exactly)" default answer "" with icon note buttons {"Export"} default button "Export")
display dialog "Choose where the exported notes should be saved:" with icon note buttons {"Choose Folder"} default button "Choose Folder"
set exportFolder to (choose folder) as string
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
-- Get an HTML file to save the note in. We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
global exportFolder
set strLength to the length of noteName
if strLength > 250 then
set noteName to text 1 thru 250 of noteName
end if
set fileName to (exportFolder & replaceText(":", "_", noteName) & ".html")
return fileName
end noteNameToFilePath
tell application "Notes"
activate
repeat with theFolder in folders of default account
if name of theFolder = requestedFolder then
repeat with theNote in every note of theFolder
set noteLocked to password protected of theNote as boolean
set modDate to modification date of theNote as date
set creDate to creation date of theNote as date
if not noteLocked then
set filepath to noteNameToFilePath(name of theNote as string) of me
set noteFile to open for access filepath with write permission
set theText to body of theNote as string
-- set eof of openFile to 0
write theText to noteFile as «class utf8»
close access noteFile
tell application "Finder"
set modification date of file (filepath) to modDate
end tell
end if
end repeat
end if
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment