forked from ksalzke/miscellaneous-omnifocus-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noteToSubtasks.omnijs
90 lines (76 loc) · 2.7 KB
/
noteToSubtasks.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
84
85
86
87
88
89
90
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Kaitlin Salzke",
"identifier": "com.KaitlinSalzke.noteToSubtasks",
"version": "1.2",
"description": "Converts the TaskPaper-formatted note of the selected task into subtasks. Note '[ ]' is recognised as '-' to allow for notes to be generated as part of a TaskPaper template.",
"label": "TaskPaper Note to Subtasks",
"shortLabel": "Note to Subtasks"
}*/
var _ = (function() {
var action = new PlugIn.Action(function(selection, sender) {
task = selection.tasks[0];
// get current perspective
var startingPerspective = document.windows[0].perspective;
// ignore everything up to first '[ ]' or '- ' in TaskPaper
var regex = /^.*?(?=(\[\s\]|-\s))/gs;
var taskpaper = task.note.replace(regex, "");
// get note and replace underscores before "[" with tabs -- needed because Shortcut removes tabs from Drafts template
taskpaper = taskpaper.replace(/(_)+(?=\[)/g, function(match) {
let underscoreLength = match.length;
let replacement = "\t".repeat(underscoreLength);
return replacement;
});
// replace '[ ]' with '-'
taskpaper = taskpaper.replace(/\[\s\]/g, " - ");
//create list of tags from original task
var tagArray = [];
task.tags.forEach(function(tag) {
tagArray.push(tag.name);
});
tagList = tagArray.join(", ");
// get ID of (topmost) creating note
var createdById;
var regex = /\[CREATEDBY: omnifocus:\/\/\/task\/(.+)\]/;
var regexResult = regex.exec(task.note);
if (regexResult == null) {
createdById = task.id.primaryKey;
} else createdById = regexResult[1];
// add checklist tag, parent tags, and creating note brackets
taskpaper = taskpaper.replace(
/(\-.*)$/gm,
"$1 @tags(Checklist," +
tagList +
")\n[CREATEDBY: omnifocus:///task/" +
createdById +
"]"
);
// replace '( )' with '[ ]'
taskpaper = taskpaper.replace(/\(\s\)/g, "[ ]");
// replace '< >' with '( )'
taskpaper = taskpaper.replace(/\<\s\>/g, "( )");
// build URL to paste tasks
var pasteUrlStr =
"omnifocus:///paste?target=/task/" +
encodeURIComponent(task.id.primaryKey) +
"&content=" +
encodeURIComponent(taskpaper);
//open URL (generating subtasks)
URL.fromString(pasteUrlStr).call(() => {});
// return to starting perspective
var perspectiveUrlStr =
"omnifocus:///perspective/" +
encodeURIComponent(startingPerspective.name);
URL.fromString(perspectiveUrlStr).call(() => {});
});
action.validate = function(selection, sender) {
if (selection.tasks.length === 1) {
return true;
} else {
return false;
}
};
return action;
})();
_;