|
// jshint esversion:6 |
|
(function() { |
|
var app = Application("OmniFocus"); //This assumes that OmniFocus is installed on MacOS |
|
app.includeStandardAdditions = true; |
|
var doc = app.defaultDocument; |
|
|
|
var perspectiveName = doc.documentWindows.at(0).perspectiveName(); |
|
if (perspectiveName != "Projects") { |
|
app.displayDialog("[Error] Selected prospective must be 'Projects'"); |
|
return; |
|
} |
|
|
|
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.") { |
|
return; |
|
} else { |
|
//app.displayDialog("[Exception] Content Selected index [" + i + "] is not a task: " + e); |
|
//return; |
|
} |
|
} |
|
} |
|
|
|
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()) |
|
var 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) { |
|
//The current tag order and proper task order don't match, so go ahead and change the order by applying the correct order |
|
totalReorderedTasks = totalReorderedTasks + 1; |
|
app.remove(task.value().tags(), { from: task.value().tags }); |
|
app.add(properlyOrderedTags, { to: task.value().tags }); |
|
} |
|
} |
|
|
|
function getRootTag(tag){ |
|
var 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 = []; |
|
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 + " tags from " + totalTasks + " candidate objects"); |
|
})(); |