-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTaskToCalendarIcs.omnifocusjs
175 lines (151 loc) · 4.92 KB
/
addTaskToCalendarIcs.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
/* global PlugIn */
/* eslint spaced-comment: ["error", "always", { "exceptions": ["{"] }] */
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Mark E. Schill",
"identifier": "net.cmschill.addTaskToCalendarIcs",
"version": "0.0.1",
"description": "This action will create an iCal (ics) file in the OmniFocus documents folder, matching the parameters of the currently selected task. On macOS, the file will be automatically opened causing the Calendar application to present a new event dialog.",
"label": "Schedule Task (ICS)",
"shortLabel": "iCal File",
"image": "calendar.badge.plus"
}*/
(() => {
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
}
);
}
function dateToCalDateStamp(date) {
var dateStamp = date
.toISOString()
.split("-")
.join("")
.split(":")
.join("")
.replace(".", "");
dateStamp = dateStamp.substring(0, dateStamp.length - 4) + "Z";
return dateStamp;
}
const action = new PlugIn.Action(async function (selection, sender) {
task = selection.tasks[0];
taskID = task.id.primaryKey;
taskURL = "omnifocus:///task/" + taskID;
taskTitle = task.name;
taskNote = task.note;
if (!taskNote) {
taskNote = "";
} else {
// Replace CR+LF and LF newlines with \n for iCal format
taskNote = taskNote.replace(/\n/g, "\\n");
}
/*
taskDueDateStamp = dateToCalDateStamp(task.dueDate)
taskDuration = task.estimatedMinutes
if (!taskDuration){taskDuration = 60}
taskEndDate = task.dueDate
taskEndDate = new Date(
taskEndDate.setMinutes(taskEndDate.getMinutes() + taskDuration)
)
taskEndDateStamp = dateToCalDateStamp(taskEndDate)
dateStamp = dateToCalDateStamp(new Date())
uid = uuidv4()
*/
eventStartDate = new Date();
eventEndDate = new Date();
taskDuration = task.estimatedMinutes;
console.log("duration:", taskDuration);
if (task.dueDate) {
taskEndDate = task.dueDate;
taskStartDate = task.dueDate;
eventStartDate = new Date(
taskStartDate.setMinutes(taskStartDate.getMinutes() - taskDuration)
);
eventEndDate = new Date(taskEndDate.setMinutes(taskEndDate.getMinutes()));
}
uid = uuidv4();
console.log("eventStartDate:", eventStartDate);
console.log("eventEndDate:", eventEndDate);
var startDateField = new Form.Field.Date(
"startDateTime",
"Starting Time",
eventStartDate
);
var endDateField = new Form.Field.Date(
"endDateTime",
"Ending Time",
eventEndDate
);
var inputForm = new Form();
inputForm.addField(startDateField);
inputForm.addField(endDateField);
var formPrompt = "Enter event details:";
var buttonTitle = "Continue";
var formPromise = inputForm.show(formPrompt, buttonTitle);
inputForm.validate = function (formObject) {
startDateObject = formObject.values["startDateTime"];
endDateObject = formObject.values["endDateTime"];
//--> "2019-01-19T08:00:00.000Z"
// IS THE PROVIDED DATE LATER THAN NOW?
startDateStatus = startDateObject && startDateObject > new Date();
console.log("startDateStatus", startDateStatus);
endDateStatus = endDateObject && endDateObject > new Date();
console.log("EndDateStatus", endDateStatus);
positiveTime = startDateObject < endDateObject;
console.log("positiveTime", positiveTime);
validation =
startDateStatus && endDateStatus && positiveTime ? true : false;
return validation;
};
formPromise.then(function (formObject) {
eventStartDateStamp = dateToCalDateStamp(
formObject.values["startDateTime"]
);
eventEndDateStamp = dateToCalDateStamp(formObject.values["endDateTime"]);
currentDateStamp = dateToCalDateStamp(new Date());
calEventString = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//omni-automation.com
METHOD:PUBLISH
BEGIN:VEVENT
SUMMARY:${taskTitle}
UID:${uid}
SEQUENCE:1711996889
DTSTART:${eventStartDateStamp}
DTEND:${eventEndDateStamp}
DTSTAMP:${currentDateStamp}
STATUS:CONFIRMED
CATEGORIES:Block Scheduling
LOCATION:OmniFocus
DESCRIPTION:${taskNote}
URL:${taskURL}
BEGIN:VALARM
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:P
ACTION:DISPLAY
END:VALARM
END:VEVENT
END:VCALENDAR`;
console.log(calEventString);
filename = "event.ics";
textData = Data.fromString(calEventString);
wrapper = FileWrapper.withContents(filename, textData);
folderURL = URL.documentsDirectory;
fileURL = folderURL.appendingPathComponent(filename);
wrapper.write(fileURL, [FileWrapper.WritingOptions.Atomic], null);
if (Device.current.mac) {
fileURL.open();
}
});
});
action.validate = function (selection, sender) {
return selection.tasks.length === 1;
};
return action;
})();