Here’s a worked example.
I create a template based on the one you shared in the original post. In it I have added three tags, but I have added them in the format +{tag}+
. The double square brackets defines a cross-link in Drafts, and so could get confusing very quickly. Also you had variations in your tags between including spaces between the tag and the square brackets; I’ve standardised on no spacing in the delimiter string.
Expand to show example template
This is the template: 2C3231AD-E047-421C-B268-4A55016B9AFE
---
In the morning I did:
+{morning}+
In the afternoon I did:
+{afternoon}+
In the evening I did:
+{evening}+
---
At the top of my template, you’ll see it has a random looking string. This is the unique identifier of the draft (you can copy it in the info section of any draft), and is shown here just for illustrative purposes. The key parts really are the inclusion of those substitution tags.
I’ve created a function that will scan through the template for tags. Using those tags it will find drafts created today with each tag (sorted in creation order). The content of those drafts is concatenated using single new line characters between them. Should none be found “Nothing.” will be used instead. The content is then substituted in for the tags in the template (multiple times if the tag is in the template multiple times).
The processed drafts then have the tags removed and are put into the archive. I think you would be better off not removing the tags, so if you want to eliminate that behaviour add “//
” to the start of the drMatched.removeTag(strDailyTag);
line near the end of the function. The reason is then you know what was added to what section if there is a later issue. There’s certainly no harm in retaining it as the searching is only being carried out in the inbox, things being in the archive being assumed to have already been processed.
Expand to show constructDaily Function
function constructDaily(p_strTemplateUUID)
{
//Get the tag matches
let drTemplate = Draft.find(p_strTemplateUUID);
let regexDailyTag = /\+{(.*)}\+/g;
let astrDailyTags = drTemplate.content.match(regexDailyTag);
astrDailyTags = astrDailyTags.map(strDailyTag => {return strDailyTag.replace(/\+{|}\+/g, '');});
//Set the output
let strOutput = drTemplate.content;
//Process each substitution tag in the template
astrDailyTags.map(strDailyTag => {
//Build query date for today
let qdCreated = new QueryDate();
qdCreated.field = "created"
qdCreated.type = "absolute"
qdCreated.date = Date.today();
//Make a temporary Workspace
let wsDaily = Workspace.create();
wsDaily.setInboxSort("created", false, false);
//Technically we could have something generate a future dated draft, so
//for the sake of one line, let's be specific.
wsDaily.startDate = qdCreated;
wsDaily.endDate = qdCreated;
//Apply a tag filter
wsDaily.tagFilter = strDailyTag;
//Get the drafts in the Workspace and concatenate their content
let adrDailyTagged = wsDaily.query("inbox");
let strDailyTaggedContent = adrDailyTagged.map(drMatch => {return drMatch.content}).join("\n");
//Account for no tags that day
if(strDailyTaggedContent == "") strDailyTaggedContent = "Nothing.";
//Substitute in the concatenated results to the output
let reDailyTag = new RegExp("\\+{" + strDailyTag + "}\\+","g");
strOutput = strOutput.replace(reDailyTag,strDailyTaggedContent);
//Untag and archive the drafts in the workspace
adrDailyTagged.map(drMatched => {
drMatched.removeTag(strDailyTag);
drMatched.isArchived = true;
drMatched.update();
})
});
//Output built from template content to new draft
let drNew = Draft.create();
drNew.content = strOutput;
drNew.update();
//Load the new draft
editor.load(drNew);
editor.activate();
}
The function takes a single parameter. The UUID of the template draft, the one I showed in the template so you could see how it is used later … here. Therefore for the example template, my function call looks like this:
constructDaily("2C3231AD-E047-421C-B268-4A55016B9AFE");
Putting the function and the function call together in an action is just a case of adding them sequentially into a single script step within an action.
To use this, you just need to edit the action and set that UUID to the UUID of your template draft. The function call line is the last line of the action step.
Hope that helps.