forked from rxhanson/Rectangle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
48 lines (41 loc) · 1.58 KB
/
AppDelegate.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
//
// AppDelegate.swift
// RectangleLauncher
//
// Created by Ryan Hanson on 6/14/19.
// Copyright © 2019 Ryan Hanson. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
if #available(macOS 13, *) {
terminate()
return
}
let mainAppIdentifier = "com.knollsoft.Rectangle"
let running = NSWorkspace.shared.runningApplications
let isRunning = !running.filter({$0.bundleIdentifier == mainAppIdentifier}).isEmpty
if isRunning {
self.terminate()
} else {
let killNotification = Notification.Name("killLauncher")
DistributedNotificationCenter.default().addObserver(self,
selector: #selector(self.terminate),
name: killNotification,
object: mainAppIdentifier)
let path = Bundle.main.bundlePath as NSString
var components = path.pathComponents
components.removeLast()
components.removeLast()
components.removeLast()
components.append("MacOS")
components.append("Rectangle")
let newPath = NSString.path(withComponents: components)
NSWorkspace.shared.launchApplication(newPath)
}
}
@objc func terminate() {
NSApp.terminate(nil)
}
}