Skip to content

Instantly share code, notes, and snippets.

@keng42
Created January 15, 2021 05:34
Show Gist options
  • Save keng42/46d5850366fd1946ceba0e1df5b5cf92 to your computer and use it in GitHub Desktop.
Save keng42/46d5850366fd1946ceba0e1df5b5cf92 to your computer and use it in GitHub Desktop.
//
// main.swift
// Shell Notification Center
//
// Created by Keng Lee on 2021/1/15.
//
import Foundation
import Combine
var bag = Set<AnyCancellable>()
let dnc = DistributedNotificationCenter.default()
func shell(_ n: Notification) {
let task = Process()
task.launchPath = "/bin/zsh"
task.arguments = ["/Users/keng42/.shnc/main.sh", n.name.rawValue]
task.launch()
}
let events = [
"com.apple.screenIsLocked",
"com.apple.screenIsUnlocked",
"com.apple.screensaver.didstart",
"com.apple.screensaver.willstop",
"com.apple.screensaver.didstop",
]
events.forEach { ev in
dnc.publisher(for: Notification.Name(rawValue: ev))
.sink(receiveValue: shell)
.store(in: &bag)
}
print("Shell Notification Center Started")
// keepalive
dispatchMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment