How to backup Apple Notes on macOS, using Automator
3 min read

How to backup Apple Notes on macOS, using Automator

Learn how to save a copy of your Apple Notes, so that you do not lose them in case they are deleted from your app.
How to backup Apple Notes on macOS, using Automator

Apple Notes are an easy way to keep your thoughts organized and synced among all your Apple devices. And it is free. Notes can be synced via iCloud, or, if you are concerned about privacy, kept On My Mac only.

The Apple Notes app is my favorite note-taking app because it is fast, easy to use, and easy to sync.

If, like me, you use Apple Notes to organize your work and your life, you want to be sure that you do not risk to lose them accidentally, either because of your mistake or because of a bug.

Unfortunately, there is no backup function in Apple Notes. You can copy the notes database (try to look in the folder ~/Library/Group Containers/group.com.apple.notes/), but if you want to backup a readable format of your notes, you must export each single note as PDF from the File menu, and when you have hundreds of notes, that quickly becomes annoying.

Automator to the rescue

Given that Notes is scriptable -to learn what you can do via script, open Script Editor on macOS and File - Open dictionary... and then choose Notes.app- which means that you can use Automator to export every note one by one.

In fact, you can just copy this code and past it in a Run AppleScript step of a new workflow in Automator:

set exportFolder to (choose folder) as string

-- 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

-- 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"
    
    repeat with theNote in notes of default account
        
        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
        
        set noteID to id of theNote as string
        set oldDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "/"
        set theArray to every text item of noteID
        set AppleScript's text item delimiters to oldDelimiters
        
        if length of theArray > 4 then
            
            -- the last part of the string should contain the ID
            -- e.g. x-coredata://39376962-AA58-4676-9F0E-6376C665FDB6/ICNote/p599
            set noteID to item 5 of theArray
        else
            set noteID to ""
        end if
        
        if not noteLocked then
            
            set fileName to ("[" & noteID & "] " & (name of theNote as string)) as string
            set filepath to noteNameToFilePath(fileName) of me
            set noteFile to open for access filepath with write permission
            set theText to body of theNote as string
            write theText to noteFile as Unicode text
            
            close access noteFile
            
            tell application "Finder"
                
                set modification date of file (filepath) to modDate
            end tell
        end if
        
    end repeat
    
end tell

Then start the workflow. It will ask for the folder where to save your notes, and then it will (slowly) create an HTML file for each note in your app.

I am not the creator of this script, but I cannot find the original source anymore 🤷‍♂️.

Subscribe to a curated newsletter

Receive an email every week with curated content about Dart and Flutter.

See previous issues of the newsletter.