-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessMonitor.swift
53 lines (44 loc) · 1.21 KB
/
ProcessMonitor.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
//* code by k
import Cocoa
import Foundation
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
func action() {
DispatchQueue.global().async {
if let file = Bundle.main.url(forResource: "patch", withExtension:"py") {
_ = shell("python3", file.path)
}
}
}
class ProcessMonitor {
static let Shared = ProcessMonitor()
func run() {
runOnce()
NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.didLaunchApplicationNotification,
object: nil,
queue: nil,
using: notificationRecieved
)
}
func runOnce() {
action()
}
func notificationRecieved(noti: Notification) {
if let proc = noti.userInfo as? [String: Any] {
let name = proc["NSApplicationName"] as! String
let pid = proc["NSApplicationProcessIdentifier"] as! Int
if name == "Finder" {
print("Finder:", name, pid)
action()
}
}
}
}