Prompt to choose between different templates

Hi, I don’t have a lot of skills with JS but I’m trying to learn a bit through this wonderful app that is Drafts.
I use a few actions to have predefined templates that I use at work and some for personal purpose. I’d like to group them into one action that would prompt what template I’d like to use and then have it displayed in Drafts.

Here is an example of a script that I’m using to create one template:

  Create new draft with template text and tag assigned.

// create template
const template = `# Meeting with:

**Other attendees:**

## Notes

## Results


// create the draft
var d = Draft.create();
d.content = template;

// load in editor and focus for editing

I have 17 actions like this with diferent templates. As I said before, I’d like to group them in one action that would prompt what template I’d like to use and then have it displayed in Drafts. Please, can anyone help?

My approach would be to put the templates in separate text files for ease of editing and revision.

Your script would have an array of the template names (which would also match the file names exactly). This would be used to populate a prompt with addSelect.

Based on the selection, read the appropriate file and set d.contents .

1 Like

For ease of maintenance, I would be inclined to store the templates in drafts, all of which have a common tag (e.g. “Template”).

Make the first line of each Draft the template name, and the rest of the draft the actual template.

You can then search for all Drafts with the relevant tag. Display the first lines in the prompt, and then use the template based on the choice from the prompt.

If you need a new template just create the new draft. No changes to the code because the search will automatically find it.

It’s the same process I used in my Group email action.


I also keep a list of templates as actions. The templates generally have a script that pre-populates the title with a date. I have created the following action that brings up a prompt asking for the template to use: Template action

This might satsify your needs