Automating Bear notes backup

Posted Jun 5, 2021

All note apps suck in some way or another. Apple Notes is too basic, Notion is too slow, Obsidian is too intimidating. For me, Bear currently strikes a nice balance between usability (well designed apps) and openness (first-class Markdown support, easy export). Bear is not perfect; it’s Apple-only, and the editor lacks some Markdown features, but I use it daily and like it a lot (and they’re apparently working on both issues).

As my collection of notes grows, it should naturally become part of my backup process, which consists of a homegrown shell script that backs up a number of things, including databases, cloud storage and personal files. While Bear has a handy backup/restore feature in both the macOS and iOS app, it has no CLI I can use from my script. The only supported way to make a backup is through its GUI.

Sure, you could easily make a copy of the underlying SQLite database, but there’s no guarantee Bear can open it a year down the line after updates have migrated the database schema, and it’s pretty unintuitive to work with directly should Bear no longer be around. Bear’s own backups, meanwhile, are just zips of TextBundle files, which makes recovery easy both in- and outside of Bear.

So here’s an AppleScript for macOS that automates the clicks and keystrokes needed to back up notes through Bear. It can be run directly from a terminal or shell script and waits around for the backup process to complete, so the file will be ready when the script exits. The whole thing does rely on some pretty shaky assumptions about the Bear interface, but GUI automation is, as always, notoriously brittle.

Important: Make sure the backup directory exists. It will not be created for you.

#!/usr/bin/osascript

set backupPath to "~/Desktop/"
set osVersion to {system attribute "sys1", system attribute "sys2", system attribute "sys3"}

-- Launch/switch to Bear
set previousFrontmostApp to (path to frontmost application) as text
activate application "Bear"
repeat until application "Bear" is running
    delay 0.5
end repeat
delay 0.5

tell application "System Events"
    tell process "Bear"
        click menu item "Backup Notes..." of menu "File" of menu bar 1

        -- Wait for the file dialog to appear
        repeat until window "Please choose the location where the notes will be saved" exists
            delay 0.5
        end repeat
        delay 0.5

        tell window "Please choose the location where the notes will be saved"
            -- Use the Cmd+Shift+G "go to folder" shortcut
            keystroke "g" using {command down, shift down}

            -- Wait for the "go to folder" window to appear
            repeat until sheet 1 exists
                delay 0.5
            end repeat
            delay 0.5

            -- Navigate to the backup folder
            tell sheet 1
                if item 1 of osVersion < 12 then
                    -- Classic "go to folder" dialog
                    set value of combo box 1 to backupPath
                    click button "Go"
                else
                    -- Monterey updated "go to folder" to a Spotlight-esque command palette
                    set value of text field 1 to backupPath
                    key code 36
                end if
            end tell

            -- Wait for the "go to folder" window to close
            repeat while sheet 1 exists
                delay 0.5
            end repeat
            delay 0.5

            click button "Export notes"
        end tell

        -- Wait for the untitled backup progress window to appear, then close
        repeat until window "Window" exists
            delay 0.5
        end repeat
        repeat while window "Window" exists
            delay 0.5
        end repeat
    end tell
end tell

-- Switch back to previously activated app
activate application previousFrontmostApp

backup-bear-notes.applescript on GitHub

Update 2022-03-14: Updated script to be Monterey-compatible.