Get the contents of a note from Apple Notes programmatically [duplicate]

I use the current Notes app on my iOS devices. It syncs nicely to my desktop. However there I prefer to work on text files usually in the command line.

Particularly I am looking for a way to export notes to a text format and to create or append a note from a text file from the command line.

Is there any information how to interact with these notes?

I found two possible ways to interact, but both turned out useless for my case:

  1. In iOS it is possible to create an action that converts notes to markdown. Unfortunately attachments get converted to links that lead nowhere. My notes contain lots of links from Safari, therefore it is not useful for me. Also wasn't able to find a way to define this action on desktop.

  2. I recalled Notes could also store in email accounts. Tried that. Turns out I can't select this destination from the sharing button. Also despite being HTML in the mail folder formatting text is no longer supported.

Description of my workflow

On desktop I do most coding in vim. This is my preferred way to edit text. Each project is managed by git. Usually in each project I have a markdown files in a folder containing ideas and notes to the code. These files I would like to make in some way available in the Notes app. I don't need to have all features of markdown, but some highlighting and inclusion of links would be nice.

On Mobile I do most research. I prefer to use the notes app as I can add text snippets and URLs quickly to any notes. So any app exporting the notes would need to reasonable export them.

Currently, I copy and paste between the two formats. This is not very satisfying. I imagine if I could get some way to import/export to the Notes app I could write scripts that would allow me to sync my local markdown notes.


Solution 1:

There is an application called Note2txt which will convert your notes from the Notes app to text files. One file for each note. It creates a file for all your notes and writes them into a directory of your choice. It runs pretty quickly taking about 30 seconds for my 700+ Notes.

Solution 2:

It appears that Notes are stored in a SQLite DB in the folder:

~/Library/Containers/com.apple.notes/Data/Library/Notes

The sqlite3 command is included with MacOS.

Who knows what format the notes are in -- I doubt it's markdown -- so you'll probably have to convert between this internal format and markdown. Also, there are other files in the directory that SQLite might or might not maintain and any files in the directory may be accessed at any time by the system daemons that sync things with iCloud. Not sure if SQLite supports file locking directly.

EDIT:

You seem to have found nothing in those folders. I am on Catalina, and when I look at NotesV7.storedata I see multiple tables:

ATRANSACTION        ZATTACHMENT         ZNOTEBODY           Z_MODELCACHE      
ATRANSACTIONSTRING  ZFOLDER             ZOFFLINEACTION      Z_PRIMARYKEY

And in ZNOTE I see what look like note titles and other info and ZNOTEBODY has HTML-formatted notes. So that would answer the question: it evidently stores notes internally in HTML format.

EDIT2:

OK, I only see notes in those tables that correspond to On My Mac notes, not iCloud notes.

Solution 3:

There's also the mac_apt MacOS Artifact Parsing Tool - It provdides for extraction of Notes, amoungst many other data types. The authour has also written a blog post on extracting Notes from the Notes DB.