Action

Apply Template

Posted by agiletortoise, Last update 6 months ago

NOTE: For more details on using templates, see the related tip document in the Forum

Prompt to select from a list of existing drafts with the tag “template” assigned, then apply that draft as a template to the current draft. This will assign tags and syntax from the template, and also insert the current content of the draft either at the end of the template or at the location of a <|> placeholder in the 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.

When run, if “Omit First Line” is selected in the prompt, the first line will be removed from the template. This allows you to use a friendly name for the template as the first line of the template without having it included in the new drafts created.

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

Steps

  • script

    // defaults
    const omitFirstLineDefault = true
    const placeholder = "<|>"
    
    // 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.queryString = ""
    	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 = "Apply Template"
    	p.message = "Select a template. The template will be applied to the current draft. The existing content of the draft will replace the first <|> placeholder found in the template, or be appended to the end if no placeholder found."
    
    	p.addSwitch("omitTitle", "Omit First Line", omitFirstLineDefault)
    	
    	let ix = 0
    	for (let d of drafts) {
    		p.addButton(d.displayTitle, ix)
    		ix++
    	}
    	
    	if (!p.show()) {
    		return false
    	}
    	
    	// get the selected template draft
    	let selectedIndex = p.buttonPressed
    	let template = drafts[selectedIndex]
    	let content = template.content
    	let omitTitle = p.fieldValues["omitTitle"]
    
    	if (omitTitle) {
    		let lines = content.split('\n')
    		if (lines.length > 0) {
    			lines.shift()
    			content = lines.join('\n').replace(/^\s+/,"")
    		}
    	}
    
    	// apply to draft and assign content/tags/syntax
    	let d = draft
    	for (let tag of template.tags) {
    		if (tag != "template") {
    			d.addTag(tag)
    		}
    	}
    	d.syntax = template.syntax
    	content = d.processTemplate(content)
    	if (content.includes(placeholder)) {
    		content = content.replace(placeholder, d.content)
    	}
    	else {
    		content = content + "\n" + d.content
    	}
    	d.content = content
    	
    	d.update()
    	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.