-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgetcalendar.swift
258 lines (201 loc) · 7.47 KB
/
getcalendar.swift
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/swift
/*
Simple script to grab calendar events via the Apple Calendar
app, filter them and then format them as a tana-paste format
blob of text. Use a keyboard macro accelerator or other
mechanism to get this into Tana
If you run this from terminal you can do:
./getcalendar | pbcopy
And then simply paste the result into Tana.
Accepts arguments, many of which should be quoted on the cmd line:
-calendar "name of calendar" <default: Calendar >
-me "name of yourself in meeting attendees" <default: Me >
Script removes yourself from meeting attendees. If you leave it as default,
you will be included since Calendar doesn't use "me" as a name anywhere
-ignore "event title to ignore" (can be repeated)
<starts out as defaults "Block", "Lunch", "DNS/Focus time", "DNS/Lunch", "Focus time" >
-solo (if present, include meetings with a single attendee)
-one2one "#[[tag name for one2one meetings]]" <default #[[1:1]] >
-meeting "#[[tag name for regular meetings]]" <default: #[[meeting]] >
-person "#[[tag name for attendees]]" <default: #person >
Example:
./getcalendar.swift -me "Brett Adam" -person "#people"
*/
import Foundation
import EventKit
// TODO: make these parameters somehow!
var calendar_name = "Calendar"
var self_name = "Me"
var titles_to_ignore = ["Block", "Lunch", "DNS/Focus time", "DNS/Lunch", "Focus time" ]
var ignore_solo_meetings = true
var meeting_tag = "#meeting"
var one2one_tag = "#[[1:1]]"
var person_tag = "#person"
var next:String? = nil
var args = CommandLine.arguments
args.removeFirst()
for argument in args {
if next != nil {
switch next {
case "-calendar":
calendar_name = argument
case "-me":
self_name = argument
case "-ignore":
titles_to_ignore.append(argument)
case "-solo":
ignore_solo_meetings = false
case "-one2one":
one2one_tag = argument
case "-meeting":
meeting_tag = argument
case "-person":
person_tag = argument
default:
fputs("Unknown argument " + next! + "\n", stderr)
exit(1)
}
next = nil
}
else {
next = argument
}
}
// tana-paste format to follow...
print("%%tana%%")
// some structs we need for working with the data
struct Event: Codable {
let title: String
let startDate: String
let endDate: String
let location: String?
let attendees: [Attendee]?
let notes: String?
let recurrence: [String]?
init(title: String, startDate:String, endDate:String,
attendees:[Attendee]?=nil, notes:String?=nil, location:String?=nil, recurrence:[String]?=nil) {
self.title = title
self.startDate = startDate
self.endDate = endDate
self.location = location
self.attendees = attendees
self.notes = notes
self.recurrence = recurrence
}
}
struct Attendee: Codable {
let name: String
let url: URL?
init(name:String, url:URL?=nil) {
self.name = name
self.url = url
}
}
let eventStore = EKEventStore()
// Ask for Calendar access, reset in casse we messed up earlier
// See stackoverflow.
// IMPORTANT: you must grant Calendar access to whatever script runner
// you are using. This can be tricky to pull off since you cannot do
// this manually in System Preferences (Grr)
// For example, Keyboard Maestro needs a "helper" the first time
// See https://forum.keyboardmaestro.com/t/icalbuddy-doesnt-work-within-keyboard-maestro-mojave-calendar-permissions/15446/40?u=brett_adam
eventStore.requestAccess(to: .event) { (granted, error) in
if granted {
eventStore.reset()
// go on managing reminders
}
}
let today = Calendar.current.startOfDay(for: Date())
let startDate = Calendar.current.date(byAdding: .day, value: 0, to: today)!
let endDate = Calendar.current.date(byAdding: .day, value: +1, to: today)!
let calendars = eventStore.calendars(for: .event )
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
let events = eventStore.events(matching: predicate)
// filter all the events we don't care about
// and narrow to our single relevant calendar
let filteredEvents = events.filter { event in
event.calendar.title == calendar_name
&& !titles_to_ignore.contains(event.title)
}
// Now map the event array to JSON strings
let eventArray = filteredEvents.map { event in
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd H:mm"
let startDateString = formatter.string(from: event.startDate)
let endDateString = formatter.string(from: event.endDate)
// Filter the notes field
var notes = event.notes
if let range = event.notes?.range(of: "~==========================~") {
notes = String(event.notes![..<range.lowerBound])
}
if let range = event.notes?.range(of: "is inviting you to") {
notes = String(event.notes![..<range.lowerBound])
}
if let range = event.notes?.range(of: "Microsoft Teams meeting") {
notes = String(event.notes![..<range.lowerBound])
}
if let range = event.notes?.range(of: "[Autodesk]\r\nBrett Adam") {
notes = String(event.notes![..<range.lowerBound])
}
notes = notes?.trimmingCharacters(in: .whitespacesAndNewlines)
// crunch the attendees list
let attendees = event.attendees?.compactMap { (attendee: EKParticipant) -> Attendee? in
guard let name = attendee.name else {
return nil
}
return Attendee(name: name
//, url: attendee.url
)
}
let recurrence: [String]? = event.hasRecurrenceRules ? event.recurrenceRules!.map { rule in
return rule.description
} : nil
return Event(title: event.title
, startDate: startDateString, endDate: endDateString
, attendees: attendees, notes: notes
, location: event.location
, recurrence: recurrence
)
}
for event in eventArray {
var node_tag = meeting_tag
var name = "- " + event.title + " with "
var attendee_field = " - Attendees:: \n"
var count = 0
for attendee in event.attendees ?? [] {
count += 1
name = name + " [[" + attendee.name + "]]"
if attendee.name != self_name {
attendee_field = attendee_field + " - [[" + attendee.name + person_tag + "]]\n"
}
else {
attendee_field = attendee_field + ""
}
}
if count == 0 && ignore_solo_meetings {
continue;
}
if count == 2 {
node_tag = one2one_tag
}
print(name + " " + node_tag)
fputs(attendee_field, stdout)
print(" - Start time:: [[date:" + String(event.startDate) + "/" + String(event.endDate) + "]]")
// spit out JSON for further examination or to feed RAW to some other API
// emitJSON(event:event);
}
// OLD JSON code if you want to see raw data
// or feed to this to other consuming tools via API
func emitJSON(event:Event) {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let jsonData = try encoder.encode(event)
if let jsonString = String(data: jsonData, encoding: .utf8) {
print("- " + event.title + " with #meeting")
print(" - JSON:: \(jsonString)")
}
} catch {
print("Error encoding JSON: \(error)")
}
}