It occurs to me that @pat’s concept here, while different than the OP’s requirement, contains a framework that could be adapted for the purpose of exporting notes to files.
(And I realize that somewhere buried in my archives I believe I worked up a runCommand export-to-file routine once, several years ago, I’ll look for that.)