forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
90 lines (78 loc) · 3.26 KB
/
Copy pathAppDelegate.swift
File metadata and controls
90 lines (78 loc) · 3.26 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
private var window: NSWindow!
private var windowForego = false
private var windowLoaded = false
private let logger = USLogger(#fileID)
@IBOutlet weak var enbaleNativeLogger: NSMenuItem!
@objc func handleGetURL(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
// issue: https://developer.apple.com/forums/thread/697217
// sendExtensionMessage(name: "URL_SCHEME_STARTED")
// if open panel is already open, stop processing the URL scheme
if NSApplication.shared.keyWindow?.accessibilityIdentifier() == "open-panel" { return }
// handle URL scheme
if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue,
let url = URL(string: urlString) {
logger?.info("\(#function, privacy: .public) - \(urlString, privacy: .public)")
if url.host == "changesavelocation" {
// avoid opening the panel repeatedly and playing unnecessary warning sounds
if NSApplication.shared.keyWindow?.identifier?.rawValue == "changeSaveLocation" { return }
if windowLoaded {
let viewController = window.contentViewController as? ViewController
viewController?.changeSaveLocation(nil)
} else {
windowForego = true
schemeChangeSaveLocation()
}
}
}
}
// https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428623-applicationwillfinishlaunching/
func applicationWillFinishLaunching(_ notification: Notification) {
// https://developer.apple.com/documentation/foundation/nsappleeventmanager/1416131-seteventhandler
NSAppleEventManager.shared().setEventHandler(
self,
andSelector: #selector(handleGetURL(event:replyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
}
// https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428385-applicationdidfinishlaunching
func applicationDidFinishLaunching(_ notification: Notification) {
// Initialize menu items
enbaleNativeLogger.state = Preferences.enableLogger ? .on : .off
// Whether to initialize the main view
logger?.debug("\(#function, privacy: .public) - windowForego: \(self.windowForego, privacy: .public)")
if windowForego { return }
let storyboard = NSStoryboard(name: "View", bundle: Bundle.main)
let windowController = storyboard.instantiateInitialController() as! NSWindowController
// let viewController = windowController.contentViewController as! ViewController
window = windowController.window
window.setIsVisible(true)
windowLoaded = true
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
return true
}
func applicationWillTerminate(_ notification: Notification) {
// Insert code here to tear down your application
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@IBAction func enableLogger(_ sender: NSMenuItem) {
if sender.state == .on {
Preferences.enableLogger = false
sender.state = .off
} else {
Preferences.enableLogger = true
sender.state = .on
}
}
@IBAction func applicationHelp(_ sender: NSMenuItem) {
if let url = URL(string: "https://github.com/quoid/userscripts") {
NSWorkspace.shared.open(url)
}
}
}