Workflow for automating templates?

I’m wondering if there’s a way to automate updating a template file in Drafts, specifically the ones you can edit in the Finder/iCloud Drive. For example, I have a template with the academic semester week (week 1, week 2, etc.), and I create notes that pull in these templates as tags. Since the semester week changes every Monday, I essentially have to update the template manually.

I’d love to automate this process (perhaps using Shortcuts by grabbing Calendar data), so that I can automatically update the content of a template file through automation. I know there’s a Drafts Shortcut action called “Process Template,” but this doesn’t seem to update or write to a template—only processes it.

Is there a method or workflow that would allow me to actually update the template content in Drafts using Shortcuts or other automation? (I imagine I could do this using Keyboard Maestro, but since I often work on iPads, I need an approach that isn’t Mac specific.)

Any advice or guidance on this would be welcome. Thanks.

1 Like

Not sure the details of the workflow you have set up.

In my mind, a template should have placeholders for piece of information that change which get filled in at the time you use the template to create a note, not have statically updated values. How are you loading and using these templates? Could the value be figured out and inserted at the time you are creating the draft from the template?

If so, how is that value calculated? It doesn’t sound like it something that is strictly date-related.

Right, thanks for your thoughts. Here’s an example of my setup:

When I create a draft, some tags are pulled in dynamically from placeholder text within Drafts templates. These tags often specify details like academic year, semester, or week of the semester (or some combination of these). While they’re date-related, they aren’t strict calendar dates. So, when I create a new note using an action, it populates this draft with the relevant, updated information from the templates. However, at the moment, I’m manually updating these template values in Drafts each week.

For similar cases, I’ve set up automations with Data Jar, where Shortcuts pull information from a calendar event to update values automatically in Data Jar. Ideally, I’d love to replicate this process in Drafts.

If there are alternative ways to keep this information up-to-date, I’m open to suggestions! I’m aware I could set up Shortcuts that directly create notes in Drafts with dynamic tags, but I find it faster to work with templates in Drafts via Actions rather than running Shortcuts for each note creation, which is why I initially asked about automating the template files directly.

Drafts scripting can work with calendar events so it sounds like that might be possible.

At the moment, everything is high level. It is probably worth setting out a worked out example of a best case scenario where you define where your source data is, and what you want a Drafts action to produce when run. The more precise the detail, the better, but you can make sure to simplify the output format if you have lots of static text.

Thanks @sylumer

Drafts scripting can work with calendar events so it sounds like that might be possible.

Could you provide more details on how Drafts scripting can work with calendar events to automate this?

Basically, my best-case setup would allow me to update templates automatically. I teach multiple courses each semester, and I use workspaces to organize notes by course. I also have actions set up to create new notes within these workspaces. I mentioned Shortcuts and Data Jar because I use Data Jar to pull the current week info automatically, which I then incorporate into items I add in other apps in my workflow, like OmniFocus and Bear. Ideally, I’d like to do something similar in Drafts to avoid the manual update.

The dynamic information I want to include is in the form of tags like “spring semester/week 1,” “/week 2,” and so on. This helps me track notes by academic week, which is useful as I accumulate notes over the semester across different courses and projects. Currently, I manually update the Drafts template to include the week tag. (To be extra clear, when I say “template,” I’m referring to the files in the iCloud Drive/Drafts/Library/Templates folder that I call with [[template|path]].) Ideally, though, I’d like to automate this by pulling the academic week information directly from my calendar, so I can skip the manual update process. That said, I’m open to other solutions—there may be an easier or more obvious way to accomplish this that I haven’t considered.

While I can achieve this using Shortcuts, calling a Shortcuts action from Drafts to create a new note feels slower than simply using a Drafts action that relies on Drafts templating.

Details, including examples are in the scripting documentation.

How is that information stored in your calendar? Is there an event each week in your main calendar of a specific name format? Is there an all day entry every Monday I a special calendar called “Semester”? Is the week number in the title, is it the only content, is it always the first number on the second line, is it somewhere in the event, but always prefixes with "WEEK: "?

When you run the Shortcut to update Data Jar, that same shortcut could overwrite your template files in the Drafts directory. It wouldn’t be following the good practice Greg noted around variable data items vs templates, but it would keep your broader logic together and save you digging into the JavaScript (which can be fun I might add) for an option that may simply be a little more practical for you to maintain.

You could use a shortcut to pass data from Data Jar back into Drafts as template tags for the template at run time, but the performance is going to be noticeably slower.

How is that information stored in your calendar? Is there an event each week in your main calendar of a specific name format? Is there an all day entry every Monday I a special calendar called “Semester”? Is the week number in the title, is it the only content, is it always the first number on the second line, is it somewhere in the event, but always prefixes with "WEEK: "?

There is indeed an iCal calendar (“Academic Calendar”) and every Monday-Friday there is a five-day, all-day event simply titled “Week 1” “Week 2” etc.

When you run the Shortcut to update Data Jar, that same shortcut could overwrite your template files in the Drafts directory. It wouldn’t be following the good practice Greg noted around variable data items vs templates, but it would keep your broader logic together and save you digging into the JavaScript (which can be fun I might add) for an option that may simply be a little more practical for you to maintain.

Bad practice aside, which Shortcut can overwrite the template files? As I write this, I suspect it’s perhaps not a Shortcut from Drafts, but a system one that can overwrite a text file?

Unfortunately, I don’t know the first thing about JavaScript (except for installing Drafts Actions that rely on it and slightly editing them…), so as fun as it might be, it’s not an option for me!

The Shortcuts action to save a file is “Save File”. Pass in the text to save (e.g. From a Text action) and overwrite the template file.

PSA: Backup before experimenting.

1 Like

Perfect—thanks @sylumer! That’s what I was trying to accomplish and it works as intended.