|
var app = Application('OmniFocus'); |
|
app.includeStandardAdditions = true; |
|
var doc = app.defaultDocument; |
|
|
|
var properRootTagOrder = getActiveRootOrderedTags(doc) |
|
|
|
var content = doc.documentWindows.at(0).content; |
|
var selectedTree = content.selectedTrees(); |
|
var selectedLen = selectedTree.length; |
|
var totalTasks = 0; |
|
var totalReorderedTasks = 0; |
|
|
|
var tasks = []; |
|
for (var i=0;i < selectedLen; i++) { |
|
try{ |
|
var task = selectedTree[i]; |
|
tasks.push(task); |
|
totalTasks = totalTasks + 1; |
|
}catch(e) { |
|
if (e.message === "User canceled.") { |
|
break; //get out of the loop (end the program) |
|
} else { |
|
// app.displayDialog("[Exception] Content Selected index [" + i + "] is not a task: " + e); |
|
} |
|
} |
|
} |
|
|
|
tasks.forEach(task => { |
|
applyProperOrderToTask(task, properRootTagOrder); |
|
}); |
|
|
|
function applyProperOrderToTask(task, properRootTagOrder) { |
|
var tags = task.value().tags() |
|
var thisDictRootTags = {} |
|
tags.forEach(tag => { |
|
//app.displayDialog("tag: " + tag.name()) |
|
thisRootTag = getRootTag(tag) |
|
//app.displayDialog("root tag: " + thisRootTag.name()) |
|
if (!(thisRootTag.id() in thisDictRootTags)) { |
|
thisDictRootTags[thisRootTag.id()] = [] |
|
thisDictRootTags[thisRootTag.id()].push(tag) |
|
} else { |
|
thisDictRootTags[thisRootTag.id()].push(tag) |
|
} |
|
}); |
|
var properlyOrderedTags = [] |
|
properRootTagOrder.forEach( rootTag => { |
|
if (rootTag.id() in thisDictRootTags) { |
|
thisDictRootTags[rootTag.id()].forEach(tag => { |
|
properlyOrderedTags.push(tag) |
|
}); |
|
} |
|
}); |
|
var properlyOrderedTagsStr = tagsNamesAsString(properlyOrderedTags) |
|
var tagsStr = tagsNamesAsString(tags) |
|
if (properlyOrderedTagsStr !== tagsStr) { |
|
//TODO: Apply actual change to task |
|
//Something like this but in Javascript https://github.com/Rahlir/OmniFocusScripts/blob/31c0e78210ab069e78011597abb54a10f367f2cb/Sort%20Tags/sorttags.applescript#L15 |
|
//Logically I want to do something like this but the below does not work |
|
// task.value().tags.remove(tags) //Remove the original unsorted tags |
|
// task.value().tags.add(properlyOrderedTags) //Replace with the newly sorted orginal tags |
|
totalReorderedTasks = totalReorderedTasks + 1 |
|
app.displayDialog("task '" + task.name()+ "' ordered tags [" + properlyOrderedTagsStr + "]") |
|
} |
|
} |
|
|
|
function getRootTag(tag){ |
|
parentTag = tag.container() |
|
if (parentTag.name().trim() === "OmniFocus") { |
|
return tag |
|
} |
|
return getRootTag(parentTag) |
|
} |
|
|
|
//app.displayDialog(tagsNamesAsString(rootTags)) |
|
|
|
function getActiveRootOrderedTags() { |
|
var allTags = doc.flattenedTags(); |
|
var answer = "" |
|
var answer = [] |
|
allTags.forEach(tag => { |
|
if (tag.container() !== null) { |
|
if (tag.container().name().trim() === "OmniFocus") { |
|
if (!tag.hidden()) { |
|
answer.push(tag); |
|
} |
|
} |
|
} |
|
}); |
|
return answer |
|
} |
|
|
|
function tagsNamesAsString(tags) { |
|
if (tags === null) { |
|
return ""; |
|
} |
|
var answer = "" |
|
tags.forEach(tag => { |
|
if (answer === "") { |
|
answer = tag.name() |
|
} else { |
|
answer = answer + ", " + tag.name() |
|
} |
|
}); |
|
return answer |
|
} |
|
|
|
app.displayDialog("Reordered " + totalReorderedTasks + " tasks from " + totalTasks + " candidate tasks") |