Apple Notes Backup

You have a lot of stuff in your Apple Notes? You don't feel comfortable dragging around sqlite databases and hoping you can extract something from them?

Save this to your bin folder (or paste it in Script Editor.app minus the #! line), and run it. It'll let you pick a folder, then save every item as a tiny web page, ID-Name.html. Images and other objects are saved in the page as Base64.

  • TODO: I haven't tested complex features, most of my notes are text with a few emoji & images, and those work.
  • TODO: As noted in code, I don't reset the modification date yet, I had permission problems from AppleScript. I could probably just shell out to do that.

I love Yama's insane but working replaceText function. AS really should have a "sed" function.

#!/usr/bin/env osascript
-- Notes Export
-- by Mark Damon Hughes, https://mdhughes.tech/tools
-- based on https://yama-mac.com/en/notes_export/#script
-- but this one works!

set exportPath to POSIX path of (choose folder)
set htmlHead1 to "<!DOCTYPE HTML>" & linefeed & "<html lang='en-US' dir='ltr'><head><title>"
set htmlHead2 to "</title>" & linefeed & "<meta http-equiv='content-type' content='text/html; charset=UTF-8'></head><body>" & linefeed

-- Simple text replacing
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

on purifyName(noteName, noteId)
    set strLength to the length of noteName
    if strLength > 250 then
        set noteName to text 1 thru 250 of noteName
    end if
    return replaceText("/", "_", replaceText(":", "_", noteId & "-" & noteName)) & ".html"
end purifyName

tell application "Notes"
    repeat with theNote in notes of default account
        set noteName to name of theNote as string
        -- 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
        -- TODO: set modDate on file

        -- ID in format x-coredata://uuid/ICNote/p000
        set oldDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "/"
        set noteId to id of theNote as string
        set idArray to every text item of noteId
        set noteId to last text item of idArray
        set AppleScript's text item delimiters to oldDelimiters

        set fileName to purifyName(noteName, noteId) of me
        set filePath to (exportPath & fileName)
        if not (exists filePath) then
            make new file at exportFolder with properties {name:fileName}
        end if
        set noteFile to open for access filePath with write permission
        set theText to body of theNote as string
        write htmlHead1 to noteFile as «class utf8»
        write noteName to noteFile as «class utf8»
        write htmlHead2 to noteFile as «class utf8»
        write theText to noteFile as «class utf8»
        close access noteFile
    end repeat
end tell