Bear: Opening Random Notes

  • A simple Python script that selects and opens a random Bear note.
  • Tested on macOS Catalina, with Bear version 1.7.10.
  • Breakdown:
    1. Identify the Bear database location.
    2. Execute the SQLite command for selecting a random note.
      • The function RAND() generates a random value for each row in the table.
      • The ORDER BY clause sorts all rows in the table by the random number generated with the RAND() function.
      • The LIMIT clause picks the first row in the result.
    3. Open the note using Bear’s callback URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
# encoding: utf-8

import os
import sqlite3
import webbrowser


path = os.path.expanduser(
'~/Library/Group Containers/9K33E3U3T4.net.shinyfrog.bear/Application Data/')

conn = sqlite3.connect(path + 'database.sqlite')
c = conn.cursor()

result = c.execute('''
SELECT ZUNIQUEIDENTIFIER FROM ZSFNOTE ORDER BY RANDOM() LIMIT 1;
''').fetchone()[0]


base_url = "bear://x-callback-url/open-note?id="
url = base_url + result

webbrowser.open_new(url)