Action

Add to List

Posted by agiletortoise, Last update almost 2 years ago

UPDATES

almost 2 years ago

Re-written for more flexible list creation.

show all updates...

almost 2 years ago

Re-written for more flexible list creation.

over 3 years ago

Added support for adding multiple items (one per line)

Takes the lines in the current draft, and appends them to a running list of items. Intended for easily updating lists you will refer to later, for things like movies you want to watch, or errands you have to run.

The action identifies lists based on the tag “list”. It will prompt for you to select from any existing drafts you have with the tag “list”, or you can enter the name for a new list to create and append to.

A more detailed explanation of usage is available in the related forum tip.

Steps

  • script

    // Configurable options
    
    // set to name of tag to use to identify list drafts
    const tag = "list"
    
    // After adding an item to the list, should the new list be opened in editor
    const openList = false
    
    
  • script

    // will be used to hold the target list draft
    let listDraft
    
    // finds existing "list" drafts
    function getListDrafts() {
    	let drafts = Draft.query("", "all", [tag])
    	return drafts
    }
    
    // prompt for options
    let f = () => {
    	let listDrafts = getListDrafts()
    	let p = new Prompt()
    	p.title = "Select List"
    	p.isCancellable = true
    	if (listDrafts.length > 0) {
    		p.message = "Select from existing list, or create new one below"
    		for (let d of listDrafts) {
    			p.addButton(d.displayTitle, d)
    		}
    	}
    	else {
    		p.message = "No lists found. To create a new list, enter a name for the list below"
    	}
    	p.addTextField("newList", "Create new list", "", {"placeholder": "List Name"})
    	p.addButton("Create New List", "new", true)
    	if (p.show()) {
    		if (p.buttonPressed == "new") {
    			if (p.fieldValues["newList"].length > 0) {
    				let d = new Draft()
    				d.content = `# ${p.fieldValues["newList"]}
    `
    				d.addTag(tag)
    				d.isArchived = true
    				d.update()
    				listDraft = d
    				return true
    			}
    			else {
    				alert("Invalid list name. Please enter a name for the new list to continue.")
    				return false
    			}
    		}
    		else { // existing list
    			listDraft = p.buttonPressed
    			return true
    		}
    	}
    	else { // user cancelled
    		return false
    	}
    }
    
    if (!f()) {
    	context.cancel()
    }
  • script

    // process lines of current draft
    let lines = draft.lines.map(x => x.trim())
    let items = []
    for (let line of lines) {
    	if (line.length > 0) {
    		items.push(line)
    	}
    }
    
    // add new list lines to list draft and update it
    listDraft.append(items.map(x => `- [ ] ${x}`).join("\n"))
    listDraft.update()
    if (openList) {
    	editor.load(listDraft)
    }
    app.displayInfoMessage(`${items.length} item(s) added to list`)

Options

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