Drafts Action Directory

Add to list

Posted by dchar, Last updated: 2018-07-04 15:39:21 UTC

Append to running lists in Drafts.

Action directory: https://actions.getdrafts.com/a/1Gd

Discuss in Drafts Forum

See it in action

This action is used to append items to running lists in Drafts. Useful for things like capturing movies you want to watch or stuff to pick up at the grocery store.

Based on an example action by @agiletortoise.

Usage

As a example, create a new list of movies to watch.

  • Create a new draft and enter Godfather
  • Tap the Add to list action
  • Press the button Create a new list
  • Enter Movies in the text field and press Ok

This creates a new draft with a title ## Movies and a tag lists:

## Movies

- [ ] Godfather

Add another movie.

  • Create a new draft and enter Jaws
  • Tap the Add to list action
  • Press the button Movies

This updates the movie list draft:

## Movies

- [ ] Godfather
- [ ] Jaws

These lists can be found by filtering the draft list by the tag lists. Create a Workspace for “Lists” with a tag filter for easy access.

Customizations

Several customizations are available in the first script action.

Change the variable appendItem to switch between append and prepend modes:

// true to append
// false to prepend
const appendItem = true;

Version History

  • Add option to append or prepend items to a list.

  • Collect list names from tagged drafts.

  • Create new lists inside the action.

  • Based on an example action by @agiletortoise.

Install

Steps

  • script

    const listTag = 'lists';
    
    // true to append
    // false to prepend
    const appendItem = true;
    
  • script

    (() => {
    
    	const getLists = () => {
    		return Draft.query('', 'inbox', [listTag])
    			.reduce((o, d) => {
    				const m = d.content.match(/## ([^\n]+)/);
    				if (m) { o[m[1].trim()] = d; }
    				return o;
    			}, {});
    	};
    
    	const sortcase = (xs) => {
    		return xs.sort((a,b) => {
    			return a.toLowerCase().localeCompare(b.toLowerCase());
    		})
    	}
    
    	const split = (s, delim) => {
    		const i = s.indexOf(delim);
    		return [
    			s.substr(0, (i>=0) ? i : s.length),
    			(i>=0) ? s.substr(i) : ''
    		]
    	};
    
    	const append = (s, item) => {
    		s = s.replace(/\n$/, '');
    		return [s, item].join('\n');
    	};
    
    	const prepend = (s, item) => {
    		const xs = split(s, '- [');
    		xs[0] = xs[0].replace(/\n$/, '');
    		return [xs[0], item, xs[1]].join('\n');
    	};
    
    	const main = () => {
    
    		if (draft.hasTag(listTag)) {
    			alert('Cannot run this action on a list draft.');
    			context.fail();
    			return;
    		}
    
    		// prompt to select a list
    		let p = Prompt.create();
    		p.title = 'Add item to list';
    
    		const lists = getLists();
    		sortcase(Object.keys(lists)).map(c => p.addButton(c));
    
    		// Give the user an option to create a new list
    		p.addButton('Create a new list');
    
    		if (!p.show()) {
    			context.fail();
    			return;
    		}
    
    		let d = lists[p.buttonPressed];
    		if (d === undefined) {
    			p = Prompt.create();
    			p.title = 'Create a new list';
    			p.addTextField('listName', 'New list', '');
    			p.addButton('Ok');
    
    			if (!p.show()) {
    				context.fail();
    				return;				
    			}
    
    			const name = p.fieldValues['listName'].trim();
    			if (name.length == 0) {
    				alert('Invalid list name.');
    				context.fail();
    				return;				
    			}
    
    			d = Draft.create();
    			d.content = `## ${name} \n`;
    			d.addTag(listTag);
    		}
    
    		const item = '- [ ] ' + draft.content.trim();
    		const fn = appendItem ? append : prepend;
    		d.content = fn(d.content, item);
    		d.update();
    	}
    
    	main();
    
    })();
    

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.