Adding date info in MMDDYYYY format to note template


#1

Hello. This is probably a very basic question but I know next to nothing about scripting. I need help creating a draft template that can be easily run via an action. I have an existing template (see below) that sets the note title and then some very basic standard sections. How would I add the date in MMDDYYYY format to the end of the top line of the “Bi-Weekly Meeting” line? I’m targeting the creation date of the draft.

I’m guessing it has something to do with [[created]]?

/*
Create new draft with template text and tag assigned.
*/

// create template
const template = `Bi-Weekly Meeting
- Action Items
- Notes
`;

// create the draft
var d = Draft.create();
d.content = template;
d.addTag("meeting");
d.update()

// load in editor and focus for editing
editor.load(d);
editor.focus()

#2

You can get the date like:

const d = Date.today().toString('MMddYYYY');

#3

You can use the Drafts template engine to accomplish this by using the date tag in the template string and running your template through processTemplate before assigning it to the content of the draft. Like the below:

/*
Create new draft with template text and tag assigned.
*/

// create template
const template = `Bi-Weekly Meeting
- Action Items
- Notes
- Date: [[date|%m-%d-%Y]]
`;

// create the draft
var d = Draft.create();
d.content = d.processTemplate(template); // <-NOTE CHANGE
d.addTag("meeting");
d.update()

// load in editor and focus for editing
editor.load(d);
editor.focus()

#4

Thanks for the responses. If I wanted to append it onto the first line “Bi-Weekly Meeting DATE”, I’m assuming I just place the [[DATE | %M-%D-%Y]] at the end of the first line?

Is there a benefit to using the ‘ProcessTemplate’ function?


#5

That worked. Thanks again!


#6

I think the main benefit would be if you were using multiple Drafts tags as they would be processed in one single sweep/instruction.

As it stands, there are pros and cons to both approaches (based on scripting preferences and familiarity with Drafts specific functionality vs. JavaScript), but I don’t think either approach is appreciably a ‘better’ one than the other.