TIP: Quickly Manage Running Lists with the "Add to List" Action

Javascript’s standard Array.map can take an existing function on a single string, and apply it to a whole list of strings.

For example:

d.content = d.content + currentContent
.split('\n')
.map(x => '- [ ] ' + x)
.join('\n'),

Used in context:

Full source for an Add *All* to List variant – click to expand
(() => {
    'use strict';

    // Based on an original script by @agiletortoise

    /*
      Ask for a category, and append to a 
      tagged list for that category
    */

    // EDIT THIS
    // setup categories available
    const
        categories = ['Movies', 'TV Shows', 'Blog Ideas'],

        // EDIT TAG
        // tag to assign to list drafts
        listTag = 'lists',

        // grab text
        currentContent = draft.content.trim(),

        // A prompt for selecting an item from a list.
        p = categories.reduce(

            // Accumulator updates - a button added for 
            // each category in the list:
            (prompt, buttonName) => {
                prompt.addButton(buttonName);
                return prompt;
            },

            // Initial accumulator: a prompt with properties:
            Object.assign(
                Prompt.create(), {
                    title: 'Select list:',
                    message: 'Ask for a category, and append' +
                        ' to a tagged list for that category',
                    isCancellable: true
                }
            )
        );

    return p.show() ? (() => {
        const
            category = p.buttonPressed,

            // query for list drafts...
            drafts = Draft.query(category, 'inbox', [listTag]),

            // index of first draft that matches a condition
            iDraft = drafts.findIndex(
                draft => draft.content.startsWith('## ' + category)
            ),

            d = iDraft !== -1 ? (
                drafts[iDraft]
            ) : Object.assign(
                Draft.create(), {
                    content: '## ' + category + '\n\n'
                });

        return (
            // tag and update content
            d.addTag(listTag),

            d.content = d.content + currentContent
            .split('\n')
            .map(x => '- [ ] ' + x)
            .join('\n'),

            d.update(),
            console.log(true)
        );
    })() : (
        context.cancel(),
        console.log(false)
    );
})();
4 Likes