Last active
January 8, 2025 08:26
-
-
Save kzaremski/08bf13f1c6062d69acc6de24a33efaa8 to your computer and use it in GitHub Desktop.
Export Apple Notes From Database --- This script aims to open a NoteStore.sqlite and related database files from Apple Notes on MacOS in order to manually extract note contents into standard formats for use in backing up and data recovery. It is not feature rich. The techniques employed by this script may be eventually integrated into my main Ap…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# export-apple-notes-from-database.py | |
# Konstantin Zaremski | |
# December 21, 2024 | |
# | |
# DESCRIPTION: | |
# This script aims to open a NoteStore.sqlite and related database files from | |
# Apple Notes on MacOS in order to manually extract note contents into standard | |
# formats for use in backing up and data recovery. It is not feature rich. The | |
# techniques employed by this script may be eventually integrated into my main | |
# Apple Notes Exporter MacOS app: | |
# https://github.com/kzaremski/apple-notes-exporter | |
# | |
# USAGE: | |
# (1) Make sure that some version of Python 3 is installed on your MacOS system. | |
# (2) Run the script using Python 3 with no arguments. | |
# | |
# $ python3 export-apple-notes-from-database.py | |
# | |
# (3) When prompted, drag in the NoteStore.sqlite file or provide a path. | |
# | |
# NoteStore.sqlite Path? /Users/kzaremski/Desktop/ane/NoteStore.sqlite | |
# | |
# (4) When prompted, drag in an output folder or provide a path. | |
# | |
# NoteStore.sqlite Path? /Users/kzaremski/Desktop/ane/NoteStore.sqlite | |
# | |
# (5) The script will load the database, decompress the note data, and then save | |
# it in the output folder. Note folder structure or dates will not be | |
# preserved. | |
# | |
# (*) Alternatively, run the script with two arguments: the input NoteStore.sqlite | |
# file followed by the output directory path. | |
# | |
# $ python3 export-apple-notes-from-database.py ./NoteStore.sqlite ./out | |
# | |
# LICENSE: | |
# Copyright (c) 2024 Konstantin Zaremski - All Rights Reserved. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# | |
# Import dependencies | |
import sys | |
import io | |
import sqlite3 | |
import gzip | |
import os | |
class Note: | |
def __init__(self, key, data): | |
self.key = key | |
self.data = data | |
self.title = None | |
self.content = None | |
def decompress(data): | |
return gzip.decompress(data) | |
# Function adapted from ChrLipp (Christian Lipp) - original code available at https://github.com/ChrLipp/notes-import | |
# The original function was part of a Gradle-based project. | |
def getPlainText(input): | |
start_index = input.find(b'\x08\x00\x10\x00\x1a') | |
if start_index == -1: | |
raise ValueError("Magic string not found in input") | |
start_index = input.find(b'\x12', start_index + 1) + 2 | |
if start_index == -1: | |
raise ValueError("Start index after '\\x12' not found in input") | |
end_index = input.find(b'\x04\x08\x00\x10\x00\x10\x00\x1a\x04\x08\x00', start_index) | |
if end_index == -1: | |
raise ValueError("End index not found in input") | |
return input[start_index:end_index] | |
def main(noteStoreSqlitePath, outputFolderPath): | |
conn = sqlite3.connect(noteStoreSqlitePath) | |
cursor = conn.cursor() | |
notes = [] | |
for row in cursor.execute(''' | |
SELECT | |
Z_PK AS key, | |
ZDATA AS data | |
FROM ZICNOTEDATA; | |
'''): | |
key = row[0] | |
data = row[1] | |
notes.append(Note(key, data)) | |
conn.close() | |
for note in notes: | |
data = note.data | |
try: | |
decompressed = decompress(note.data) | |
note.content = getPlainText(decompressed).decode() | |
except Exception: | |
continue | |
note.title = (note.content[:50] if len(note.content) > 50 else note.content).replace('\n', '') | |
os.makedirs(outputFolderPath, exist_ok=True) | |
for note in notes: | |
if note.content is not None: | |
filename = f'{note.title} - {note.key}.txt'.strip() | |
file_path = os.path.join(outputFolderPath, filename) | |
try: | |
with open(file_path, 'w', encoding='utf-8') as file: | |
file.write(note.content) | |
print(f"Saved note to: {file_path}") | |
except Exception as e: | |
print(f"Failed to save note with key {note.key}: {e}") | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
noteStoreSqlitePath = sys.argv[1] | |
else: | |
noteStoreSqlitePath = input('NoteStore.sqlite Path? ').strip() | |
if len(sys.argv) > 2: | |
outputFolderPath = sys.argv[2] | |
else: | |
outputFolderPath = input('Output Directory Path? ').strip() | |
main(noteStoreSqlitePath, outputFolderPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment