Hi @complexpoint !
Well, let me be frank. My ulterior goal here is to learn how to repurpose or rewrite some code that Jesse wrote. I should have been upfront about that, as Jesse probably knows the answer off the top of his head. Jesse never fails to inspire and impress me with his coding. That is not flattery, that is honesty.
I like to tinker, and when I look at your code, I am in awe, and simultaneously, at a loss. Your code is beyond me—which hinders my (admittedly) feeble ability to tinker.
Jessie’s code is clearer to me—I can sometimes alter it to suit my purposes (bonus!). Along the way, I learn a bit more about JavaScript (double bonus!!). And I get to make a new TaskPaper macro (triple bonus!!!). As a tinkerer, this type of stuff is important to me.
So, following is the code that inspired my current tinkering project. If you, or Jesse, have any suggestions, my tinkering fingers are listening.
This code works with two opened TaskPaper documents. It grabs the project that the text cursor is in (the cursor could be on a project, task or sub-task) and it moves it to the top of the second TaskPaper document. It works like a charm, as long as both TaskPaper documents have text in them (which is fine for me, as I never use a completely empty TaskPaper document).
function TaskPaperExportContext(editor, options) {
let outline = editor.outline
let selection = editor.selection
let currentItem = selection.startItem
let owningProjects = outline.evaluateItemPath('ancestor-or-self::project', currentItem)
let owningProject = owningProjects[owningProjects.length - 1]
if (owningProject) {
owningProject.removeFromParent();
return ItemSerializer.serializeItems(owningProject.branchItems)
}
}
function TaskPaperImportContext(editor, options) {
let outline = editor.outline
let imported = ItemSerializer.deserializeItems(options, outline)
outline.insertItemsBefore(imported, null)
let root = editor.outline.root
root.insertChildrenBefore(imported, root.firstChild)
}
var exportedText = Application('TaskPaper').documents[0].evaluate({
script: TaskPaperExportContext.toString()
});
if (exportedText) {
Application('TaskPaper').documents[1].evaluate({
script: TaskPaperImportContext.toString(),
withOptions: exportedText,
})
}
So the following is my tinkering goal:
Take the above code, and alter it to take a project with tasks from the Mac clipboard (or a Keyboard Maestro variable) and do what it already does (place that project and tasks at the top of the opened TaskPaper document).
I want to remove the code that selects the project (which I think is entirely within the first function). And I want to use this with one TaskPaper document instead of two. I think that I want to work solely with the code in the second function, but all of my attempts thus far have met with failure.
Once I have the above figured out, I have four new macros that will benefit from my tinkering.
Jim