-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDocumentWaiter.swift
More file actions
119 lines (96 loc) · 3.15 KB
/
DocumentWaiter.swift
File metadata and controls
119 lines (96 loc) · 3.15 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// DocumentWaiter.swift
// VisualDiffer
//
// Created by davide ficano on 30/08/11.
// Copyright (c) 2011 visualdiffer.com
//
import AppKit
import ScriptingBridge
let documentClosedNotification = NSNotification.Name("VDDocumentClosedNotification")
enum ComparisonLaunchError: Error, LocalizedError {
case launch(description: String)
case script(error: any Error)
var errorDescription: String? {
switch self {
case let .launch(description):
description
case let .script(error):
((error as NSError).userInfo["ErrorString"] as? String) ?? error.localizedDescription
}
}
}
class DocumentWaiter: NSObject, SBApplicationDelegate {
struct Options {
let waitClose: Bool
let shouldRestoreFocus: Bool
}
var leftPath: URL
var rightPath: URL
var uuid: String?
private let options: Options
private var error: (any Error)?
init(
leftPath: URL,
rightPath: URL,
options: Options
) {
self.leftPath = leftPath
self.rightPath = rightPath
self.options = options
super.init()
DistributedNotificationCenter.default.addObserver(
self,
selector: #selector(exitFromApp),
name: documentClosedNotification,
object: nil
)
}
@objc
func exitFromApp(_ notification: NSNotification) {
let uuidNotification = notification.object as? String
if uuid == uuidNotification {
CFRunLoopStop(CFRunLoopGetCurrent())
}
}
func openDocument() throws {
let activeApplication = NSWorkspace.shared.frontmostApplication
guard let app = SBApplication(bundleIdentifier: "com.visualdiffer") else {
throw ComparisonLaunchError.launch(description: "Unable to find VisualDiffer application")
}
app.delegate = self
app.activate()
// in Swift is more simple to use the dynamic approach
uuid = app.perform(
NSSelectorFromString("openDiffLeftPath:rightPath:"),
with: leftPath.path(percentEncoded: false),
with: rightPath.path(percentEncoded: false)
)?.takeRetainedValue() as? String // swiftlint:disable:this multiline_function_chains
if let error {
throw ComparisonLaunchError.script(error: error)
}
#if DEBUG
if uuid == nil {
print("UUID is null. Note to myself. If application is launched from XCode this doesn't work. Close the app and launch it from dock")
}
#endif
guard uuid != nil, options.waitClose else {
return
}
CFRunLoopRun()
if options.shouldRestoreFocus {
restoreFocus(activeApplication)
}
}
private func restoreFocus(_ activeApplication: NSRunningApplication?) {
guard let activeApplication,
!activeApplication.isTerminated else {
return
}
activeApplication.activate()
}
func eventDidFail(_: UnsafePointer<AppleEvent>, withError error: any Error) -> Any? {
self.error = error
return nil
}
}