Swift program for getting a zoom app link from current or upcoming event in calendar.
Apple script calling the program and opening zoom.
#!/usr/bin/osascript | |
set meeting to do shell script "/usr/local/bin/nextzoommtg" | |
if meeting = "" then | |
log "No meeting" | |
else | |
if application "Music" is running then | |
tell application "Music" | |
set player_state to player state | |
if player_state is playing then | |
log "Pausing music" | |
pause | |
end if | |
end tell | |
end if | |
set volume output volume 100 | |
open location meeting | |
end if |
// | |
// main.swift | |
// icalswift | |
// | |
// Created by Sergei A. Fedorov on 23.04.2020. | |
// Copyright © 2020 Sergei A. Fedorov. All rights reserved. | |
// | |
import Foundation | |
import Foundation.NSFileHandle | |
import Darwin.C.stdlib | |
import EventKit | |
do { | |
let store = EKEventStore() | |
switch EKEventStore.authorizationStatus(for: .event) { | |
case .authorized: | |
NSLog("Authorized") | |
break | |
case .restricted: | |
NSLog("Access resticted") | |
exit(1) | |
case .denied: | |
NSLog("Access denied") | |
exit(1) | |
case .notDetermined: | |
let group = DispatchGroup() | |
group.enter() | |
store.requestAccess(to: .event) { (granted: Bool, NSError) in | |
if granted { | |
NSLog("Granted") | |
} else { | |
NSLog("No access granted") | |
exit(1) | |
} | |
group.leave() | |
} | |
group.wait() | |
break | |
@unknown default: | |
break | |
} | |
let cal = Calendar.current | |
let now = Date() | |
var diff = DateComponents() | |
diff.hour = -3 | |
let today = cal.date(byAdding: diff, to: now) | |
diff.hour = 0 | |
diff.day = 1 | |
let tomorrow = cal.date(byAdding: diff, to: today!) | |
diff.day = 0 | |
diff.minute = 5 | |
let soon = cal.date(byAdding: diff, to: now) | |
let calendars = store.calendars(for: .event) | |
let formatter = DateFormatter() | |
formatter.dateStyle = .short | |
formatter.timeStyle = .short | |
NSLog("Events in range " + formatter.string(from: today!) + " - " + formatter.string(from: tomorrow!)) | |
let predicate: NSPredicate? = store.predicateForEvents(withStart: today!, end: tomorrow!, calendars:calendars) | |
let events : [EKEvent] = store.events(matching: predicate!) | |
var current = events.filter({ $0.startDate < soon! && now < $0.endDate}) | |
current.sort(by: { $0.startDate > $1.startDate }) | |
let re = try NSRegularExpression(pattern: #"https://(\S*zoom.us)/j/(\w+)(.*)$"#, options: [.anchorsMatchLines]) | |
let pwd_re = try NSRegularExpression(pattern: #"pwd=(\w+)"#, options: []) | |
for event in current { | |
if !event.isAllDay && (event.notes ?? "" != "") { | |
let notes = event.notes! | |
NSLog((event.title ?? "<no title>") + " <" + formatter.string(from: event.startDate) + ">") | |
NSLog(notes) | |
let range = NSRange(notes.startIndex..<notes.endIndex, in: notes) | |
let match = re.firstMatch(in: notes, options: [], range: range) | |
if let m = match { | |
let host = notes[Range(m.range(at: 1), in: notes)!] | |
let meeting_id = notes[Range(m.range(at: 2), in: notes)!] | |
var zoom_url = "zoommtg://" + host + "/join?confno=" + meeting_id + "&zc=0&stype=100"; | |
let line_tail_range = Range(m.range(at: 3), in: notes) | |
if let lr = line_tail_range { | |
let match = pwd_re.firstMatch(in: notes, options: [], range: NSRange(lr.lowerBound...lr.upperBound, in:notes)) | |
if let pm = match { | |
zoom_url = zoom_url + "&pwd=" + notes[Range(pm.range(at: 1), in: notes)!] | |
} | |
} | |
print(zoom_url) | |
exit(0) | |
} | |
} | |
} | |
} catch { | |
print(error.localizedDescription); | |
} |