Select all items in a project

Is there a way, through scripting, to select all items in a project? I have a Keyboard Maestro script that does stuff with a selection I’ve made manually in Taskpaper…so I am wondering if I can make that selection of all items in Project X as part of the macro too.

If the project is selected, then one approach might be to extend the selection to its whole subtree with an incantation like:

const seln = editor.selection.startItem;
editor.moveSelectionToItems(
    seln,
    undefined,
    seln.lastDescendant
);

moveSelectionToItems

1 Like

Tho a quick check reveals that we also need to supply the end point of the last item, to include it in the selection.

Adding that, and allowing for the possibility that the selected node might be a project descendant, rather than the project node itself, perhaps something like:

// isProject :: TP Item -> Bool 
const isProject = node =>
    'project' === (
        node.getAttribute('data-type')
    );
const
    seln = editor.selection.startItem,
    startItem = isProject(seln) ? (
        seln
    ) : (
        seln.ancestors.reverse().find(
            isProject
        ) || seln
    ),
    endItem = startItem.lastDescendant || (
        startItem
    );

editor.moveSelectionToItems(
    startItem,
    0,
    endItem,
    endItem.bodyString.length
);
Full JS example
(() => {
    'use strict';

    // main :: IO ()
    const main = () => {
        const ds = Application('TaskPaper').documents;
        if (0 < ds.length) {
            ds.at(0).evaluate({
                script: tp3Context.toString()
            })
        }
    };

    // ----------------- TASKPAPER CONTEXT -----------------
    const tp3Context = editor => {
        // isProject :: TP Item -> Bool 
        const isProject = node =>
            'project' === (
                node.getAttribute('data-type')
            );
        const
            seln = editor.selection.startItem,
            startItem = isProject(seln) ? (
                seln
            ) : (
                seln.ancestors.reverse().find(
                    isProject
                ) || seln
            ),
            endItem = startItem.lastDescendant || (
                startItem
            );
        editor.moveSelectionToItems(
            startItem,
            0,
            endItem,
            endItem.bodyString.length
        );
    };

    return main();
})();

Or in the GUI, of course, if the project item is selected, then:

Edit > Selection > Select Branch

⌘⇧B