mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
55 lines
1.7 KiB
Swift
55 lines
1.7 KiB
Swift
//
|
|
// AppDelegate.swift
|
|
// StatsLauncher
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 31.05.2019.
|
|
// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
import os.log
|
|
|
|
extension Notification.Name {
|
|
static let killLauncher = Notification.Name("killLauncher")
|
|
}
|
|
|
|
@NSApplicationMain
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
let mainAppIdentifier = "eu.exelban.Stats"
|
|
let runningApps = NSWorkspace.shared.runningApplications
|
|
let isRunning = !runningApps.filter { $0.bundleIdentifier == mainAppIdentifier }.isEmpty
|
|
|
|
if !isRunning {
|
|
DistributedNotificationCenter.default().addObserver(self,
|
|
selector: #selector(self.terminate),
|
|
name: .killLauncher,
|
|
object: mainAppIdentifier)
|
|
|
|
let path = Bundle.main.bundlePath as NSString
|
|
var components = path.pathComponents
|
|
components.removeLast(3)
|
|
components.append("MacOS")
|
|
components.append("Stats")
|
|
|
|
let newPath = NSString.path(withComponents: components)
|
|
NSWorkspace.shared.launchApplication(newPath)
|
|
}
|
|
else {
|
|
self.terminate()
|
|
}
|
|
}
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
// Insert code here to tear down your application
|
|
}
|
|
|
|
|
|
@objc func terminate() {
|
|
os_log("WTF")
|
|
NSApp.terminate(nil)
|
|
}
|
|
}
|
|
|