Using prompt results in New Draft with Template

Hi. I’m having trouble combining prompts with a modified version of New Draft with Template.

My goal is to have a single template file in Drafts; run an action, fill in two text prompts (project name and project description), create a new draft from the template — and in the process, insert the project name and description into the new draft where the template has placeholders.

I am running into problems with replacing the template placeholders. When I run the action (more below), I get the new draft, and the stock template tags (e.g., date and time) are correctly replaced (e.g., with the current date and time, in the right format); however, the placeholders for project name and description aren’t.

My only thought is that the script step doesn’t automatically receive the contents of the prompt, and so the tags aren’t filling in. Or maybe I’m misunderstanding how something else works.

Here are my action steps:

Here’s the contents of my template (which has just one tag, ‘template’):

project note template
project: [[name_text]]
description: [[description_text]]
folder: 
created: 2021-10-29 @ 14:47
modified: 2021-10-29 @ 14:47
---
# [[name_text]]

> [[description_text]]

## files & folders

- [[[name_text]] project folder](file:/// XXX)
- [[[name_text]] tasks](file:/// XXX /todo.txt)

And below is my script — it’s just the New Draft with Template script, except I’ve commented out the template-picking part in favor of hard-coding a specific template by UUID, and set the omitTitle variable to true in a slightly roundabout way. I am deleting the commented-out portion here for brevity.

// modified from New Draft with Template action
// defaults
let omitFirstLineDefault = true;

// select from a list of drafts tagged "template" and create new draft based on selection.

let f = () => {

	// get the selected template draft
//	let selectedIndex = p.buttonPressed;
	let template = Draft.find('854E4F67-F60E-4E46-91B1-825BF1AA2C43');
	let content = template.content;
	let omitTitle = omitFirstLineDefault;

	if (omitTitle) {
		let lines = content.split('\n');
		if (lines.length > 0) {
			lines.shift();
			content = lines.join('\n').replace(/^\s+/,"");
		}
	}

	// create new draft and assign content/tags/syntax
	let d = Draft.create();
	for (let tag of template.tags) {
		if (tag != "template") {
			d.addTag(tag);
		}
	}
	d.languageGrammar = template.languageGrammar;
	d.content = d.processTemplate(content);
	d.update();
	// load new draft
	editor.load(d);
	editor.activate();
	
	// look for <|> to set cursor location
	let loc = d.content.search("<|>");	
	editor.setText(editor.getText().replace("<|>", ""));
	editor.setSelectedRange(loc, 0);
	
	return true;
}

if (app.isPro) {
	if (!f()) {
		context.cancel();
	}
}
else {
	alert("Drafts Pro features required to use this action.")
}

Here’s the result of running the action recently — you’ll see that the custom tags are expanded, as it were, but the built-in ones are

project: [[name_text]]
description: [[description_text]]
folder: 
created: 2021-10-29 @ 18:14
modified: 2021-10-29 @ 18:14
---
# [[name_text]]

> [[description_text]]

## files & folders

- [[[name_text]] project folder](file:/// XXX)
- [[[name_text]] tasks](file:/// XXX /todo.txt)

Thanks in advance for any help!

Custom template tags are specific to one draft.

When you use a prompt action step, the result becomes a custom template tag of the current draft object that is in context for the action. It’s the equivalent of calling draft.setTemplateTag (docs) in script.

The script is process templates for a new draft (the variable d), which does not have those custom template tags set.

For this case, you would need to move those values over to the draft being processed, which you could do something like:

d.languageGrammar = template.languageGrammar;
// ADD BELOW
d.setTemplateTag("name_text", draft.getTemplateTag("name_text"));
d.setTemplateTag("description_text", draft.getTemplateTag("description_text"));
// END ADD
d.content = d.processTemplate(content);

Hopefully that makes sense.

1 Like

I think so. You’ve said as much in other posts, but I didn’t get it — I think I do now.

The Prompt actions attach their results to the active draft. That’s great if you start with draft contents. But on my case, I’m not, and so they aren’t associated with any draft.

So I have to assign them to the draft I created to hold the results of the script.

Is that about right?