Action

Build Daily

Posted by sylumer, Last update 3 months ago - Unlisted

Steps

  • script

    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();
    }
    
    constructDaily("2C3231AD-E047-421C-B268-4A55016B9AFE");

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.