Action

Add Draft to list

Posted by @FlohGro, Last update 1 day ago

created by @FlohGro / more on my Blog

Add Draft to list

Variant of the Add to List action.

This action distinguishes between checklists and normal lists and will add a markdown checkbox before the content into the list if a draft with the checklist tag is selected.
Additionally it displays the content of the draft in the prompt so it’s easier to use with the „On My Mind“ action group.

[Configuration]

Adapt the tags in the script to your personal settings in your library.
Do not use the same tag for checklists and normal lists. If you don’t want to use checklists, just set it to a string that won’t return any drafts (e.g. hallensvalfbeuylndveks)

const listTag = "3 list";
const checklistTag = "3 checklist"

If you find this useful and want to support me you can donate or buy me a coffee

Steps

  • script

    // add to list
    
    //eval(Draft.find("5531DF22-3B3C-4A3C-9087-34110492BF4D").content);
    
    
    // tag to assign to list drafts
    const listTag = "3 list";
    const checklistTag = "3 checklist"
    
    
    // grab text
    const currentContent = draft.content.trim();
    
    /////////
    var res;
    {
    let ws = Workspace.create();
    ws.tagFilter = listTag;
    ws.tagFilterRequireAll = true;
    res = ws.query("all");
    }
    res.sort((a,b) => (a.title.toUpperCase() > b.title.toUpperCase()) ? 1:-1);
    let listTitles = [];
    let p = Prompt.create();
    p.title = "select list for draft:"
    if(draft.title.length >0){
    let str = "draft: \"" + draft.title.slice(0, 97) + "..."
     + "\""
     p.message = str
    }
    else{
    p,message = "\"" + draft.lines[0] + "\""
    }
    
    for(i in res)
    {
     	p.addButton(res[i].displayTitle,res[i]);
    	listTitles.push(res[i].title);
    }
    p.addButton("CREATE NEW LIST");
    
    showSelectPrompt:
    var didSelect = p.show();
    if (didSelect == 1) {
    	if(p.buttonPressed != "CREATE NEW LIST")
    	{
    		var selectedDraft = p.buttonPressed;
    	} else {
    		var pN = Prompt.create();
    		pN.title = "create new list";
    		pN.message = "set title for new list";
    		pN.addTextField("setTitle", "set title","",[autocorrect=false, autocapitalization="words", keyboard="default",wantsFocus=true]);
    		pN.addButton("set title");
    		if(pN.show()){
    			if(pN.buttonPressed == "set title"){
    				var newListCategory = pN.fieldValues["setTitle"];
    				if (newListCategory != "") {
    					if(listTitles.includes(newListCategory)){
    						alert("list already exists, restart action and select the right list!");
    						context.cancel("user selected already existing list!")					} else {
    					var selectedDraft = Draft.create();
    					selectedDraft.content = "# " + newListCategory + "\n\n";
    					selectedDraft.languageGrammar = "Simple List";
    					}
    				} else {
    					alert("you must set a title, aborting!");
    					context.cancel("no title specified");
    				}
    			} else {
    				context.cancel("user cancelled title input");
    			}	
    		}
    	}
    
    
    var category = selectedDraft.displayTitle;
    
    var d;
    if (selectedDraft.content.startsWith("# " + category)) {
    			d = selectedDraft;
    } // else {
    
    //}
    
    d.addTag(listTag);
    let textToAppend = currentContent
    if(d.tags.includes(checklistTag)){
    textToAppend = "- [ ] " + textToAppend
    }
    
    if(d.content.endsWith("\n")){
    d.content = d.content + "\n" + textToAppend + "\n";
    } else {
    d.content = d.content + "\n" + textToAppend + "\n";
    }
    
    d.update();
    
    editor.load(d);
    } else {
    context.cancel("user cancelled add to list");
    }

Options

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