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!