So, given that Jesseâs script meets my needs, and I am able to use const on everything but itemsâis that one instance worth changing?
Of course â âwhatever worksâ has been the guiding principle since Chapter 1 of the book of Genesis 
Using constant (rather than mutable) value names mainly yields a harvest when you are composing scripts in terms of nested expressions rather than sequential statements.
When the code is essentially an IO interaction, and the bytes are flying about at the interface between the computation and some broader context (invisible to the computation itself), it really doesnât matter much.
The problem there was just that, in terms of JS syntax, you were:
- Declaring a constant name without any value attached to it. (as it happens, this has no meaning in the syntax or semantics of JS)
- trying to bind a value to that name between one pair of curly braces, and then expecting to find that name still defined between a separate set of braces later on.
{ ... }
braces delimit a context, or name-space. They nest, but donât transfer their name-bindings to later, separate, contexts.
You could, FWIW, have written something like:
Expand disclosure triangle to view JS source
function TaskPaperContextScript(editor, options) {
const outline = editor.outline;
const root = outline.root;
const text = options.text;
if ("string" === typeof text && text !== "") {
const items = ItemSerializer.deserializeItems(
options.text, outline, ItemSerializer.TEXTMimeType
);
return options.append
? (
root.appendChildren(items),
"Appended at end."
)
: (
root.insertChildrenBefore(items, root.firstChild),
"Inserted at start."
);
} else {
return "empty or not a string: options.text";
}
}
As in, for a version which returns a report, as well as producing an effect:
Expand disclosure triangle to view JS source
(() => {
"use strict";
const filePath = "~/Library/Mobile Documents/iCloud~is~workflow~my~workflows/Documents/Focus.txt"; // Path to TaskPaper file
// const filePath = "~/Desktop/Focus.taskpaper"
// If true, items will be added to the end rather than top of Inbox
const appendItems = true;
function TaskPaperContextScript(editor, options) {
const outline = editor.outline;
const root = outline.root;
const text = options.text;
if ("string" === typeof text && text !== "") {
const items = ItemSerializer.deserializeItems(
options.text, outline, ItemSerializer.TEXTMimeType
);
return options.append
? (
root.appendChildren(items),
"Appended at end."
)
: (
root.insertChildrenBefore(items, root.firstChild),
"Inserted at start."
);
} else {
return "empty or not a string: options.text";
}
}
const tp3 = Application("TaskPaper");
const strFullPath = ObjC.unwrap($(filePath).stringByExpandingTildeInPath);
const document = tp3.open(Path(strFullPath));
const result = document.evaluate({
script: TaskPaperContextScript.toString(),
withOptions: { text: "â Walk: @now", append: appendItems }
});
return (
document.save(),
result
);
})();