Archive done and @cancelled

Is there a way to expand the Archive Done command to also archive tasks tagged @cancelled? Or is there a script that performs the same archive function that I could modify to achieve this?

Searching around for previous answer I found this post:

I just added the translated script and wrapped it so that it can be run from script editor.

Awesome! Thank you.

So perhaps the updated path might be something like:

outline.evaluateItemPath(
    "//(@done or @cancelled) except " +
    "//@text = Archive://(@done or @cancelled)"
)

?

JS source of test
(() => {
    'use strict';

    const TaskPaperContext = (editor, options) => {
        const main = () => {
            const outline = editor.outline;
            let selection = editor.selection;
            const
                startItem = selection.startItem,
                endItem = selection.endItem,
                [removeExtraTags, addProjectTag] = [
                    'BRemoveExtraTagsWhenArchivingDone',
                    'BIncludeProjectWhenArchivingDone'
                ].map(k => Birch.preferences.get(k));

            outline.groupUndoAndChanges(() => {
                // isOrContains :: Item -> Item -> Bool
                const isOrContains = a =>
                    b => a === b || a.contains(b)
                const
                    archive = projectFoundOrCreated(outline)('Archive'),
                    doneItems = Item.getCommonAncestors(
                        outline.evaluateItemPath(
                            "//(@done or @cancelled) except " +
                            "//@text = Archive://(@done or @cancelled)"
                        )
                    );

                doneItems.forEach(each => {
                    // ---- PREFS :: EXTRA TAGS REMOVED ----
                    if (removeExtraTags) {
                        each.attributeNames.forEach(
                            eachName => eachName.startsWith('data-') && (
                                !['-type', '-done']
                                .some(k => eachName.endsWith(k))
                            ) && each.removeAttribute(eachName)
                        )
                    }

                    // ---- PREFS :: PROJECT TAGS ADDED ----
                    if (addProjectTag) {
                        const
                            projects = outline.evaluateItemPath(
                                'ancestor::@type=project', each
                            )
                            .map(x => x.bodyContentString)
                            .join(' / ');
                        if (0 < projects.length) {
                            each.setAttribute('data-project', projects);
                        }
                    }

                    // --------- SELECTION UPDATED ---------
                    if ([startItem, endItem].some(isOrContains(each))) {
                        const
                            previousItem = editor.getPreviousDisplayedItem(
                                startItem
                            );
                        selection = previousItem ? {
                            startItem: previousItem,
                            startOffset: -1
                        } : {
                            start: 0
                        }
                    }

                })
                return archive.insertChildrenBefore(
                    doneItems, archive.firstChild
                );
            });

            return editor.moveSelectionToItems(selection);
        };

        // projectFoundOrCreated :: TP3Outline -> String -> Bool -> TP3Item
        const projectFoundOrCreated = outline =>
            name => {
                const ps = outline.evaluateItemPath(`//@text = ${name}:`);
                return 0 < ps.length ? (
                    ps[0]
                ) : (
                    x => (outline.root.appendChildren(x), x)
                )(outline.createItem(name + ':'));
            };

        // ---
        return main();
    };

    const TaskPaper = Application('TaskPaper')
    return TaskPaper.documents[0].evaluate({
        script: TaskPaperContext.toString()
    });
})();
2 Likes