source: launchers/macosx/StartupItemApp/AppDelegate.swift @ ba853a8c

Last change on this file since ba853a8c was 6ad64d5b, checked in by meeh <meeh@…>, 20 months ago

Mac OSX Launcher: Adding the "launcher launcher" for startup functionality.

  • 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 mainAppIdentifier = "net.i2p.bootstrap-macosx.I2PLauncher"
28    let runningApps = NSWorkspace.shared.runningApplications
29    let isRunning = !runningApps.filter { $0.bundleIdentifier == mainAppIdentifier }.isEmpty
30   
31    if !isRunning {
32      DistributedNotificationCenter.default().addObserver(self,
33                                                          selector: #selector(self.terminate),
34                                                          name: .killLauncher,
35                                                          object: mainAppIdentifier)
36     
37      let path = Bundle.main.bundlePath as NSString
38      var components = path.pathComponents
39      components.removeLast()
40      components.removeLast()
41      components.removeLast()
42      components.append("MacOS")
43      components.append("I2PLauncher") //main app name
44     
45      let newPath = NSString.path(withComponents: components)
46     
47      NSWorkspace.shared.launchApplication(newPath)
48    }
49    else {
50      self.terminate()
51    }
52  }
53}
54
Note: See TracBrowser for help on using the repository browser.