Action

New Draft with Template Body

Posted by sylumer, Last update over 3 years ago - Unlisted

Prompt to select from a list of existing drafts with the tag “template” assigned, then create a new draft with the content of that template.

To create templates available in this action, simply type your template in a draft, and assigned the tag “template” to that draft. It can be in the archive or inbox. The template can contain Drafts tags to insert time stamps or other dynamic data in the template. Also, any tags other than “template” assigned to the template draft will be assigned to the new draft automatically.

If the template contains the string <|>, the <|> will be removed, and the initial cursor position in the new draft placed at that location.

Note: This action used functionality which requires a Pro subscription.

Steps

  • script

    // select from a list of drafts tagged "template" and create new draft based on selection.
    
    let f = () => {
    	// create temp workspace to query drafts
    	let workspace = Workspace.create();
    	if (!workspace) {
    		return false;
    	}
    	workspace.tagFilter = "template";
    	workspace.setAllSort("name", false, true);
    	// get list of drafts in workspace
    	let drafts = workspace.query("all");
    	
    	// check if we found any valid templates
    	if (drafts.length == 0) {
    		alert("No templates found. To make templates available to this action, create a draft with the template content and assign it the tag \"template\".");
    		return false;
    	}
    	
    	// prompt to select
    	let p = Prompt.create();
    	p.title = "New Draft with Template";
    	p.message = "Select a template. A new draft will be created based the template selected.";
    	
    	let ix = 0
    	for (let d of drafts) {
    		p.addButton(d.title, ix);
    		ix++;
    	}
    	
    	if (!p.show()) {
    		return false;
    	}
    	
    	// get the selected template draft
    	let selectedIndex = p.buttonPressed;
    	let template = drafts[selectedIndex];
    
    	// create new draft and assign content/tags/syntax
    	let d = Draft.create();
    	for (let tag of template.tags) {
    		if (tag != "template") {
    			d.addTag(tag);
    		}
    	}
    	d.languageGrammar = template.languageGrammar;
    	let astrLines = template.lines;
    	astrLines.shift();
    	d.content = draft.processTemplate(astrLines.join("\n"));
    	//d.content = d.processTemplate(template.content);
    	d.update();
    	// load new draft
    	editor.load(d);
    	editor.activate();
    	
    	// look for <|> to set cursor location
    	let loc = d.content.search("<|>");
    	if (p != -1) {
    		editor.setText(editor.getText().replace("<|>", ""));
    		editor.setSelectedRange(loc, 0);
    	}
    	
    	return true;
    }
    
    if (app.isPro) {
    	if (!f()) {
    		context.cancel();
    	}
    }
    else {
    	alert("Drafts Pro features required to use this action.")
    }

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.