Action

New Draft with Template DEBUG

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

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) {
    	    alert("Failed to create workspace");
    		return false;
    	}
    	workspace.tagFilter = "template";
    	workspace.setAllSort("name", false, true);
    	// get list of drafts in workspace
    	let drafts = workspace.query("all");
        alert(drafts.length + " templates found");
    	// check if we found any valid templates
    	if (drafts.length == 0) {
    	
            let adTemplate = Draft.queryByTitle("# Meeting Notes");
    
            adTemplate.forEach(dFound =>
            {
                alert("# Meeting Notes: TAGS = |" + dFound.tags.join("|") + "|");
                dFound.tags.forEach(strTag =>
                {
                    if(strTag.toLowerCase() == "template") alert(strTag + " is equal to template");
                    else alert(strTag + " is NOT EQUAL to 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;
    	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.