-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanupEvents.swift
More file actions
60 lines (51 loc) · 1.75 KB
/
CleanupEvents.swift
File metadata and controls
60 lines (51 loc) · 1.75 KB
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
import Foundation
import EventKit
let store = EKEventStore()
let semaphore = DispatchSemaphore(value: 0)
print("Requesting Calendar Access...")
if #available(macOS 14.0, *) {
store.requestFullAccessToEvents { granted, error in
if granted {
deleteEvents()
} else {
print("Access denied: \(String(describing: error))")
semaphore.signal()
}
}
} else {
store.requestAccess(to: .event) { granted, error in
if granted {
deleteEvents()
} else {
print("Access denied: \(String(describing: error))")
semaphore.signal()
}
}
}
func deleteEvents() {
let calendar = Calendar.current
// Search in a wide range (e.g., last year to next year)
let startDate = calendar.date(byAdding: .year, value: -1, to: Date())!
let endDate = calendar.date(byAdding: .year, value: 1, to: Date())!
let targetTitle = "2장 공부하기"
print("Searching for events with title: '\(targetTitle)'...")
let predicate = store.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
let events = store.events(matching: predicate)
let matchingEvents = events.filter { $0.title == targetTitle }
if matchingEvents.isEmpty {
print("No events found with that title.")
} else {
print("Found \(matchingEvents.count) events.")
for event in matchingEvents {
do {
try store.remove(event, span: .thisEvent)
print("Deleted: \(event.title ?? "") on \(event.startDate!)")
} catch {
print("Failed to delete event: \(error)")
}
}
print("Deletion complete.")
}
semaphore.signal()
}
semaphore.wait()