-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdateToV3.omnifocusjs
67 lines (56 loc) · 2.47 KB
/
updateToV3.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
/* global PlugIn */
/* eslint spaced-comment: ["error", "always", { "exceptions": ["{"] }] */
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Kaitlin Salzke",
"identifier": "com.KaitlinSalzke.UpdateToDependencyV3",
"version": "1.0",
}*/
(() => {
const action = new PlugIn.Action(async function (selection, sender) {
const dependencyLibrary = PlugIn.find('com.KaitlinSalzke.DependencyForOmniFocus').library('dependencyLibrary')
const dependantTag = await dependencyLibrary.getPrefTag('dependantTag')
const prerequisiteTag = await dependencyLibrary.getPrefTag('prerequisiteTag')
const syncedPrefs = dependencyLibrary.loadSyncedPrefs()
const getDependantIDs = (task) => {
const dependantTaskIDs = []
// use regex to find [DEPENDANT: taskid] matches in the notes and capture task IDs
const regex = /\[ ?DEPENDANT: omnifocus:\/\/\/task\/(.*?) ?\]/g
let regexArray = []
while ((regexArray = regex.exec(task.note)) !== null) {
// for each captured task ID
const dependantTaskId = regexArray[1]
dependantTaskIDs.push(dependantTaskId)
}
return dependantTaskIDs
}
const getPrereqIDs = (task) => {
const prereqTaskIDs = []
// use regex to find [PREREQUISITE: taskid] matches in the notes and capture task IDs
const regex = /\[ ?PREREQUISITE: omnifocus:\/\/\/task\/(.*?) ?\]/g
let regexArray = []
while ((regexArray = regex.exec(task.note)) !== null) {
// for each captured task ID
const prereqTaskId = regexArray[1]
prereqTaskIDs.push(prereqTaskId)
}
return prereqTaskIDs
}
// temporarily set pref to 'don't add note'
const addToNotePref = (syncedPrefs.read('addToNote') !== null) ? syncedPrefs.readBoolean('addToNote') : true
syncedPrefs.write('addToNote', false)
const links = dependencyLibrary.getLinks()
dependantTag.tasks.forEach(dep => getPrereqIDs(dep).forEach(prereqID => links.push([prereqID, dep.id.primaryKey])))
prerequisiteTag.tasks.forEach(prereq => getDependantIDs(prereq).forEach(depID => links.push([prereq.id.primaryKey, depID])))
syncedPrefs.write('links', links)
// set 'add to note' preference back to original state
syncedPrefs.write('addToNote', addToNotePref)
dependencyLibrary.updateDependencies()
})
action.validate = function (selection, sender) {
// validation code
return true
}
return action
})()