Files

56 lines
1.8 KiB
Swift

//
// ApplicationDelegate.swift
// CyberlockLauncher
//
// Created by Jura on 9/13/19.
// Copyright © 2019 Omicronmedia. All rights reserved.
//
import Cocoa
final class AppDelegate: NSObject, NSApplicationDelegate {
private struct Constants {
static let notification = Notification.Name(PrivadoConstants.Launcher.notificationName)
static let macosPath = "MacOS"
}
@objc func terminate() {
NSApp.terminate(nil)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
let runningApps = NSWorkspace.shared.runningApplications
let isRunning = !runningApps.filter { $0.bundleIdentifier == PrivadoConstants.Application.bundle }.isEmpty
if !isRunning {
DistributedNotificationCenter.default().addObserver(self,
selector: #selector(self.terminate),
name: Constants.notification,
object: PrivadoConstants.Launcher.bundle)
let path = Bundle.main.bundlePath as NSString
var components = path.pathComponents
components.removeLast()
components.removeLast()
components.removeLast()
components.append(Constants.macosPath)
components.append(PrivadoConstants.Application.name) //main app name
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
}
}