First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.
What I’m trying to do
I have a person template with a property type: template. When I create a person from the template I want the property type: person. The first file is the template, and the second is a person so the change in type is appropriate.
Things I have tried
The optimal place for me to effect this change is an inline javascript that is run by QuickAdd as part of the template.
I tried js quickadd but from the doc’s it won’t be able to see the property. So I tried just js using the examples on User Scripts | QuickAdd
I can’t get any of them to even work. Ideally I’d end up with params.variables.type = "person" as part of my script.
For whatever reason console.log isn’t even firing as part of a test.
For the life of me I can’t work out what I’m missing and would appreciate any help. Using inline scripts would be the perfect elegant solution for the way I plan to use templates. The minute I have to move to any external scripts I lose the value.
Thanks @dawni. Appreciated the long response. I had tried that which is why I’m heading to the js route. Using macros will be an untenable solution across more than a few templates, and I expect I’ll have somewhere between 20-50. The inline js solution is far superior for that in my case if I can get it to work.
Hey @dcb! There are two things tripping you up here, and neither of them is obvious from the docs.
First, the ```js block with module.exports is a user script, which is meant for macros. Inside a template, QuickAdd only runs ```js quickadd blocks. A plain js block just gets copied into the note, which is why nothing fired. (The example also wouldn’t run from a macro: params.app.vault.prompt"..." is a syntax error, and the prompt method is params.quickAddApi.inputPrompt.)
Second, a ```js quickadd block runs while QuickAdd builds the note’s text, before the note exists. So getActiveFile() gives you the note you had open, not the new one. Without the timer, that’s where the edit lands, which is likely the “appears briefly then lost” you saw. It’s worth checking the notes you had open while testing, since they probably picked up type: schema and publish: allow. The timer only works if the new note happens to be active when it fires, so I’d drop that version.
That said, I don’t think you need a script for this. Write the value straight into each template:
---
type: person
---
Then keep your templates out of your queries by skipping the templates folder (swap in your folder name):
Dataview: LIST FROM -"Templates" WHERE type = "person"
Bases: add the filter !file.inFolder("Templates")
That works the same across all your templates, with nothing to maintain. Keeping type: template in the template file itself would need a step after the note is created, like the macro dawni suggested, which is exactly the per-template setup you want to avoid. Filtering on the folder skips all of that.