forked from ksalzke/miscellaneous-omnifocus-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorderTags.omnijs
83 lines (72 loc) · 2.3 KB
/
reorderTags.omnijs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Kaitlin Salzke",
"identifier": "com.KaitlinSalzke.reorderTags",
"version": "1.0",
"description": "Reorders the tags of the selected task(s) based on the order in 'Tags'.",
"label": "Reorder Tags",
"shortLabel": "Reorder Tags"
}*/
var _ = (function() {
var action = new PlugIn.Action(function(selection, sender) {
// action code
// selection options: tasks, projects, folders, tags
// ***CONFIGURATION:*** set up items here. Tags will be sorted based on order in Omnifocus unless otherwise specified here.
firstTags = [];
lastTags = [
"This Week",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
"Do",
"🎯 MIT",
"☀️ MIT (Tomorrow)"
];
// if called externally (from script) generate selection object
if (typeof selection == "undefined") {
selection = document.windows[0].selection;
}
// create array of all tag names
tagNames = [];
tags.apply(tag => tagNames.push(tag));
// iterate through selected tasks
selection.tasks.forEach(function(task) {
// get assigned tags
assignedTags = task.tags;
// sort assigned tags based on tag index
sortedAssignedTags = assignedTags.sort(function(a, b) {
if (firstTags.includes(a.name)) {
aTagOrder = firstTags.indexOf(a.name);
} else if (lastTags.includes(a.name)) {
aTagOrder = tagNames.length + lastTags.indexOf(a.name) + 1;
} else {
aTagOrder = firstTags.length + tagNames.indexOf(a);
}
if (firstTags.includes(b.name)) {
bTagOrder = firstTags.indexOf(b.name);
} else if (lastTags.includes(b.name)) {
bTagOrder = tagNames.length + lastTags.indexOf(b.name) + 1;
} else {
bTagOrder = firstTags.length + tagNames.indexOf(b);
}
return aTagOrder > bTagOrder;
});
// remove all tags from task
task.removeTags(assignedTags);
// re-apply tags in order
task.addTags(sortedAssignedTags);
});
});
action.validate = function(selection, sender) {
// validation code
// selection options: tasks, projects, folders, tags
return selection.tasks.length > 0;
};
return action;
})();
_;