Using Quickadd inline scripts to set properties

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.

Been wanting to try QuickAdd and gave it a go for this. So:

  1. Click New choice and select Macro
  2. Click Template and, for the path, enter the path to your person template.
    • Set the rest as you please. Then click Done.
  3. Click Capture and set these:
    • Location > Capture to active file: enable
    • Position > Write position: Property
      • Property: Named property
      • [enter the name of your property, e.g.,] type
      • Action: Set value
    • Content > Capture format: enable
      • [enter the value that will replace template in your new note, e.g.] person
    • Set the rest as you please. Then click Done.
  4. Optionally, in the macro window:
  5. Click Done.

Go run your new macro.

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.

oh. For help debugging your script, show what you’ve got?

At the moment it’s code directly out of the examples.

module.exports = async params => {


 const project = await params.app.vault.prompt"Project Name:"
  return project
}

and it doesn’t even look to be firing. What I’d like is something like

module.exports = async params => {


 params.property.type = "schema"
}

but if I can’t even get the script to fire reliably then I don’t know what’s happening.

I have uploaded the file for completeness of context.

qa_schema.md (686 Bytes)

This is working at least with the quickadd form. I cannot get the straight js form to do anything yet. No idea what I’m missing.

```js quickadd
setTimeout( async () => {

	const activeFile = this.app.workspace.getActiveFile();

	if (activeFile) { 
		await app.fileManager.processFrontMatter(activeFile, (frontmatter) => {
			frontmatter.type = 'schema';
			frontmatter.publish = "allow"
		})
	}
},8000)

The timeout required to allow for the file to settle otherwise the changes appear briefly then are list. 8000 prob too much but it works.

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.

The inline script docs now cover both of the gotchas above: Inline scripts | QuickAdd

Let me know if it works for you!