I am running into some sort of bug (presumably mine) when I run my jxa script which is intended to look at every taskpaper window open, and return a map of every file that’s open with a list of window information for that file
however, when i have multiple windows of the same filepath open, then it seems to always return duplicate data based on the last window that was focused, rather than one per individual window. so in a case where i have “hello.txt” open in two windows where one has project A focused, and project B on the other window, it will return an array with [projectBstuff, projectBstuff] for that filepath in the map.
it works fine when the filepaths are different. thoughts?
(() => {
const TaskPaperApp = Application('TaskPaper');
const windows = TaskPaperApp.windows();
const filePathToContextNodeInfoMap = {};
windows.filter(window => window.visible()).forEach(window => {
const windowDocument = window.document();
// all ancestors too
const windowBounds = window.bounds();
const windowData = {
width: windowBounds?.width,
height: windowBounds?.height,
x: windowBounds?.x,
y: windowBounds?.y,
};
const focusedItemProjectWithAncestors = windowDocument.evaluate({
script: `(editor, options) => {
let item = editor.focusedItem;
let projects = [];
while (item) {
if (item.bodyString.trim().endsWith(':')) {
projects.unshift({
id: item.id,
title: item.bodyString.split('\\n')[0],
windowData: options.windowData
});
}
item = item.parent;
}
return projects;
}`,
withOptions: { windowData },
});
if (focusedItemProjectWithAncestors.length > 0) {
const filePath = windowDocument.file().toString();
filePathToContextNodeInfoMap[filePath] = filePathToContextNodeInfoMap[filePath] || [];
filePathToContextNodeInfoMap[filePath].push(focusedItemProjectWithAncestors);
}
});
return JSON.stringify(filePathToContextNodeInfoMap);
})();
sample output
{
"[redacted].txt": [
[
{
"id": "XyQfF4UwmwJe",
"title": "skin care planning:",
"windowData": {
"y": 28,
"width": 748,
"x": 748,
"height": 469
}
}
],
[
{
"id": "XyQfF4UwmwJe",
"title": "skin care planning:",
"windowData": {
"y": 28,
"width": 748,
"x": 0,
"height": 469
}
}
]
]
}
since the code explicitly runs the evaluation on the specific window, i’m not sure what the bug in my code is
i would guess that somehow taskpaper app resolves one editor object for each file hence the behavior exhibited here