Skip to content

Instantly share code, notes, and snippets.

@geelen geelen/_.md
Last active Mar 1, 2019

Embed
What would you like to do?
Sync a document from Bear Writer's DB to a file on disk

Sync a document from Bear Writer's DB to a file on disk

I really like Bear Writer's interface, but it stores all its notes in an SQLite DB. I spent ages rigging up a nice Markdown + Webpack pipeline for a React blog before I realised this, and rather than admit my mistake, potentially pushing forward to concentrate on the content rather than procrastinating on the pipeline, I built this.

Screenshot showing copying the doc id

Grab the Document ID from Bear, then run:

yarn add sqlite3 untildify
node sync-bear.js DOC-ID path/to/file.js

Screenshot of program running

👋 -glen.

const fs = require('fs')
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
const untildify = require('untildify')
const [_,__, docId, filename, ...rest] = process.argv
const bail = msg => {
console.log(msg)
process.exit(1)
}
if (!docId || !filename || rest.length > 0) bail("Usage: node sync-bear.js DOC-ID-FROM-BEAR src/to/output.md")
const BEAR_DB_PATH = untildify("~/Library/Containers/net.shinyfrog.bear/Data/Documents/Application Data/database.sqlite")
if (!fs.existsSync(BEAR_DB_PATH)) bail(`Bear DB not found, expecting it to be at: ${BEAR_DB_PATH}`)
const db = new sqlite3.Database(BEAR_DB_PATH)
let writing = false
const write_to_file = contents => {
process.stdout.write("\nWriting...")
writing = true
fs.writeFile(path.resolve(filename), contents, err => {
if (err) bail(`Error writing file: ${err}`)
process.stdout.write(" Done!\n")
writing = false
})
}
let last_contents = null
let since_last_update = 0
const exec_query = () => {
if (writing) return setTimeout(exec_query, 500)
process.stdout.write(".")
db.get(`select ZTEXT from ZSFNOTE where ZUNIQUEIDENTIFIER='${docId}'`, (err, {ZTEXT}) => {
if (err) bail(`Error accessing DB: ${err}`)
if (last_contents !== ZTEXT) {
process.stdout.write("!")
last_contents = ZTEXT
write_to_file(ZTEXT)
since_last_update = 0
}
setTimeout(exec_query, 100 + Math.log(++since_last_update) * 100)
})
}
exec_query()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.