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
);
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,
projectItem = isProject(seln)
? seln
: seln.ancestors.findLast(isProject);
return projectItem
? (() => {
const endItem = projectItem.lastDescendant || projectItem;
return (
editor.moveSelectionToItems(
projectItem,
0,
endItem,
endItem.bodyString.length
),
(n => `Selected ${n} project rows.`)(
editor.selection.selectedItems.length
)
);
})()
: "Cursor not in project.";
Full JS Example
(() => {
"use strict";
// main :: IO ()
const main = () => {
const doc = Application("TaskPaper").documents.at(0);
return doc.exists()
? doc.evaluate({ script })
: "No documents open in TaskPaper.";
};
// ----------------- TASKPAPER CONTEXT -----------------
const script = (editor => {
// isProject :: TP Item -> Bool
const isProject = node =>
'project' === node.getAttribute('data-type');
const
seln = editor.selection.startItem,
projectItem = isProject(seln)
? seln
: seln.ancestors.findLast(isProject);
return projectItem
? (() => {
const endItem = projectItem.lastDescendant || projectItem;
return (
// Effect,
editor.moveSelectionToItems(
projectItem,
0,
endItem,
endItem.bodyString.length
),
// and string value.
(n => `Selected ${n} project rows.`)(
editor.selection.selectedItems.length
)
);
})()
: "Cursor not in project.";
})
.toString()
// MAIN ---
return main();
})();
1 Like
Or in the GUI, of course, if the project item is selected, then:
Edit > Selection > Select Branch
⌘⇧B