-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctionLibrary.omnifocusjs
195 lines (179 loc) · 4.96 KB
/
functionLibrary.omnifocusjs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* global PlugIn Version DateComponents Calendar tags ApplyResult library Project Task folders inbox */
/* eslint spaced-comment: ["error", "always", { "exceptions": ["{"] }] */
/*{
"type": "library",
"targets": ["omnifocus"],
"identifier": "com.KaitlinSalzke.functionLibrary",
"version": "1.0"
}*/
(() => {
const functionLibrary = new PlugIn.Library(new Version('1.0'))
functionLibrary.adjustDateByDays = function (date, days) {
const timeToAdd = new DateComponents()
timeToAdd.day = days
return Calendar.current.dateByAddingDateComponents(date, timeToAdd)
}
functionLibrary.findTag = function (nameToFind) {
let targetTag = null
tags.apply(function (tag) {
if (tag.name === nameToFind) {
targetTag = tag
return ApplyResult.Stop
}
})
return targetTag
}
functionLibrary.getAllRemainingTasks = function () {
const allTasks = []
library.apply(function (item) {
if (
item instanceof Project &&
(item.status === Project.Status.Active ||
item.status === Project.Status.OnHold) &&
item.task.hasChildren
) {
item.task.children.forEach((tsk) => {
if (
tsk.taskStatus !== Task.Status.Dropped &&
tsk.taskStatus !== Task.Status.Completed
) {
allTasks.push(tsk)
}
})
}
})
return allTasks
}
functionLibrary.getContainingFolder = function (task) {
const project = task.project || task.containingProject || ''
const emptyResult = {
name: 'No Folder'
}
if (project === '') {
return emptyResult
}
let foundFolder = null
let i = 0
while (foundFolder === null && i < folders.length) {
const topFolderToCheck = folders[i]
topFolderToCheck.apply((item) => {
if (item === project) {
foundFolder = topFolderToCheck
return ApplyResult.Stop
}
})
i++
}
if (foundFolder === null) {
const result = {
name: 'No Folder'
}
return result
}
return foundFolder || emptyResult
}
functionLibrary.getParent = (task) => {
let parent = null
let project = null
if (task.containingProject == null) {
project = inbox
} else {
project = task.containingProject.task
}
project.apply((item) => {
if (item.children.includes(task)) {
parent = item
return ApplyResult.Stop
}
})
return parent
}
functionLibrary.getRootTask = function (childTask) {
let rootTask = null
let index = 0
const tasksToCheck = childTask.containingProject.task.children
while (rootTask === null && index < tasksToCheck.length) {
tasksToCheck[index].apply(function (task) {
if (task === childTask) {
rootTask = tasksToCheck[index]
return ApplyResult.Stop
}
})
index++
}
return rootTask
}
functionLibrary.getTaskWithId = function (taskId, tagToSearch) {
let foundTask = null
if (tagToSearch) {
let i = 0
while (i < tagToSearch.tasks.length) {
if (tagToSearch.tasks[i].id.primaryKey === taskId) {
return tagToSearch.tasks[i]
}
i++
}
}
library.apply((item) => {
if (item.id.primaryKey === taskId) {
foundTask = item
return ApplyResult.Stop
}
})
inbox.apply((item) => {
if (item.id.primaryKey === taskId) {
foundTask = item
return ApplyResult.Stop
}
})
return foundTask
}
functionLibrary.isStalled = function (project) {
// return false if SAL
if (project.containsSingletonActions) {
return false
}
// return false if has a next task
if (project.nextTask !== null && project.nextTask !== project.task) {
return false
}
if (!project.task.hasChildren) {
return true
}
let hasBlockedChildren = false
project.task.apply((child) => {
if (child.taskStatus === Task.Status.Blocked) {
hasBlockedChildren = true
return ApplyResult.Stop
}
})
if (hasBlockedChildren) {
return false
}
return true
}
functionLibrary.removeProjectTagsFromTasks = function (
taskArray,
projectTagArray
) {
taskArray.forEach((task) => {
// check if this is a project; if not remove tags
if (
task.project == null &&
!projectTagArray.includes(task.containingProject)
) {
task.removeTags(projectTagArray)
}
})
}
functionLibrary.removeTrailingSpaceFromName = function (task) {
task.name = task.name.replace(/ $/, '')
}
functionLibrary.replaceTag = function (tasksArray, oldTag, newTag) {
tasksArray.forEach(function (task) {
task.addTag(oldTag)
task.removeTag(newTag)
})
}
return functionLibrary
})()