TIP: Creating New Drafts with Templates

@sylumer Wow, thanks so much for taking the time to put this together. I have been working with it over the past week and it works without a hitch. :partying_face:

2 Likes

Glad to hear it’s working for you. :sunglasses:

1 Like

Posted an updated version of this action, which now includes an “Omit First Line in New Draft” flag in the prompt. If enabled, the first line of the template is stripped off before creating the new draft. This allows you to give your templates a friendly name in the first line, like “Meeting Notes Template”, that works well in the prompt, but is not included in the final output.

@agiletortoise how do I change the Omit First Line to default to True?

The action contains a single script step. At the very top of the script it contains is this:

// defaults
let omitFirstLineDefault = false;

Change the false to true.

I could’t figure it out: Is it somehow possible to set the syntax of the draft with this?

It is using the old way (pre custom syntax) for setting syntax based on the syntax of the template.

This is the relevant line:

d.languageGrammar = template.languageGrammar;
2 Likes

Ah, makes sense. Thanks!

1 Like

when I run this action, the setting shows as ‘Omit First Line in New Draft’ as switched ON, how can I change the default to OFF, and use it?

Change the first line of code in the script step.

const omitFirstLineDefault = true

to

const omitFirstLineDefault = false

That was it - Thanks a lot.

I have a very simple template for this action:

# Journal [[date]]

I’d like to have the same template add a tag that follows the pattern below, but it seems I can’t do this within the template by using the tag field up top.

journal/[[%Y]]/[[%m]]/[[%d]]

Is there a workaround for someone like me, who has never (or not yet) learned how to script? Or is there maybe a related script that I could make sense of and adapt?

BTW, I’ve been using Drafts since 2013. This is just the first time I’ve come up short finding or adapting a solution.

Thank you!

Scripting would be the way to go. Maybe something like this would work for you and be something you could follow based on your current understanding of template tags?

draft.addTag(draft.processTemplate("journal/[[date|%Y/%m/%d]]"));
draft.update();

Thank you. Is this something I would add to the template somehow? Or are you suggesting I create a new scripted action that both creates a new draft (from a template) and adds the tags?

I was assuming your existing template is added via an action that had a single Insert Text step. Take that existing action and add a second step, a Script step. Then put the two lines of script into that step and save your action.

The first step inserts your text and the second adds the tag.

On the other hand, if your template action is based on a Create Draft step instead, then you have a tag field available and can just use that same tag format from the code above in that field.

In the case of the specific “New Draft with Template” action that this thread is discussing, @sylumer’s code could be inserted as follows in the existing script:

// LOOK FOR THE BELOW LINES

// create new draft and assign content/tags/syntax
let d = Draft.create()
for (let tag of template.tags) {
	if (tag != "template") {
		d.addTag(tag)
	}
}

// ADD JUST THIS THIS LINE DIRECTLY AFTER
d.addTag(draft.processTemplate("journal/[[date|%Y/%m/%d]]"))