source: launchers/macosx/StartupItemApp/AppDelegate.swift

Last change on this file was 2af1f68, checked in by meeh <meeh@…>, 2 years ago

Mac OSX Launcher: Misc metadata updates for the StartupItemApp? sub-bundle.

  • Property mode set to 100644
File size: 1.5 KB
Line 
1//
2//  AppDelegate.swift
3//  StartupItemApp
4//
5//  Created by Mikal Villa on 21/12/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Cocoa
10
11extension Notification.Name {
12  static let killLauncher = Notification.Name("killStartupLauncher")
13}
14
15@NSApplicationMain
16class AppDelegate: NSObject {
17 
18  @objc func terminate() {
19    NSApp.terminate(nil)
20  }
21}
22
23extension AppDelegate: NSApplicationDelegate {
24 
25  func applicationDidFinishLaunching(_ aNotification: Notification) {
26   
27    let runningApps = NSWorkspace.shared.runningApplications
28    let isRunning = !runningApps.filter { $0.bundleIdentifier == Identifiers.mainApplicationBundleId }.isEmpty
29   
30    if !isRunning {
31      DistributedNotificationCenter.default().addObserver(self,
32                                                          selector: #selector(self.terminate),
33                                                          name: .killLauncher,
34                                                          object: Identifiers.mainApplicationBundleId)
35     
36      let path = Bundle.main.bundlePath as NSString
37      var components = path.pathComponents
38      components.removeLast()
39      components.removeLast()
40      components.removeLast()
41      components.append("MacOS")
42      components.append("I2PLauncher") //main app name
43     
44      let newPath = NSString.path(withComponents: components)
45     
46      NSWorkspace.shared.launchApplication(newPath)
47    }
48    else {
49      self.terminate()
50    }
51  }
52}
53
Note: See TracBrowser for help on using the repository browser.