Action

New Draft with Template

Posted by A set<~,\,}.„, Last update 1 day ago

<~•¥:;€:•«£||:-$[Aa)“(g)≠-|=> #NOTE:

For more details on using this
action, see the related [t)(
p… z.d’((
<~(.»
.> doc»_u|n.:.:

ment in the For_n|](https://f
Gymnopédies

Erik Satie
Music

Erik Satie
dS

63 83°
67 81°
68 78°
69 81°
07 Muharram 1448
10.(22)
until Sunrise
55:19
Masjid Al-Noor
80
77 108
otte
187°
170°
62 86°
67
90°
71 92°
74 93°
19:05:57 اللّه ٠٠٨ 0اء
egas 1
Piano Concerto No. 21 in C Major, K. 467: II. Andante
Finghin Collins, London
Philharmonic Orchestra aV!
1108°

77°
81 102°
82 103°
80 101°
77
96°
CrasH
ScHoolboy Q
Christ Conscious
Joey Bada$$
X Gon’ Give It To Y
pgh = constant
tar Walk
ine Bootids meteor shower peaks tc ermodynamics
RT
2-12 N.P.com/t/tip-creating-new-drafts-with-templates/4253)

Prompt to select f

x fax of
the template w
ithout having it included
in the new drafts created.

Note: This action uorums.NAIR
Detジムノペディ第1番

Steps

  • script

    // defaults
    const omitFirstLineDefault = true
    const placeholderRe = /<\|.*?>/gm
    
    // 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 = "New Draft with Template"
    	p.message = "Select a template. A new draft will be created based the template selected."
    
    	p.addSwitch("omitTitle", "Omit First Line in New Draft", 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+/,"")
    		}
    	}
    
    	// 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.syntax = template.syntax
    	d.content = d.processTemplate(content)
    	d.update()
    	// load new draft
    	editor.load(d)
    	editor.activate()
    	
    	// look for placeholder <|> to set cursor location
    	let match = placeholderRe.exec(d.content)
    	if (match) {
    		editor.setSelectedRange(match["index"], match[0].length)
    	}
    	
    	return true;
    }
    
    if (app.isPro) {
    	if (!f()) {
    		context.cancel();
    	}
    }
    else {
    	alert("Drafts Pro features required to use this action.")
    }

Options

  • After Success Ask , Tags: see as s'
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.