Here is a discussion on methods of creating new notes in projects from external sources. The method described above, of using the sync folder feature to set up an “inbox” is gone over in detail, there, as well as other approaches such as using the scratch pad feature. The only update I would have for this older post is that neither of those Mac tools I mentioned further on down, as “front ends” to the sync folder, are maintained these days. More people are using Scrivener Sync-folder + Obsidian, and Markdown files for this kind of task.
But if you do want to continue down the scripting path, it can be rewarding to do so as the above mechanisms are limited, and do have their awkward corners here and there.
The main downside to scripting against the project itself is a need to close it and reopen it, which neither of the above methods have an issue with. But that problem can be dodged by making use of the Mobile folder, which the iOS version creates and uses to insert and modify data into an open project, back on the Mac/PC.
The hardest part about that will be the binder.mob file, the rest is so dirt simple it’s barely even scripting. Here is an example project, started from Blank, that has had one single file added to it on the iPhone:
simple_mobile_folder_example.tar.gz (11.4 KB)
The “Mobile” folder created from this action looks like this:
simple_mobile_folder_example.scriv
├── Mobile
│ ├── binder.mob
│ ├── Data
│ │ ├── 35ABC720-C93A-4FA3-AB21-8FC0EBEE8502.checksums
│ │ └── A8A32240-0438-48A3-BAB3-DF0D13D3A61E
│ │ └── content.rtf
│ ├── search.indexes
│ ├── structure.cache
│ └── writing.history
You can omit everything at the top level save for the ‘binder.mob’ file, you do need to examine that and see how the inserted content.rtf file is mapped. The checksums file can also be omitted, that is only used by the iOS version itself to ensure data integrity between uses.
As for the binder.mob file, you basically want to diff it with the .scrivx to see where they differ. You want to copy the whole Binder tree from the .scrivx into it, and then make your insertion to that. You can then copy the LabelSettings, StatusSettings and ProjectTargets verbatim. I haven’t tested leaving those out, as changes to those are meant to sync, naturally (otherwise we wouldn’t include them in the .mob file). Overall, not that difficult or different from injecting something into the .scrivx XML.
The most difficult part about this (either editing the main project or using Mobile) in fact is generating RTF files, and it sounds like you have that part under control already. For anyone else coming along, if you don’t have a good library in your scripting language for it, Pandoc can generate clean and simple RTF files, and you can specify a custom header too, which can mean adopting the project default formatting (which can be extracted from the project’s settings).