-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
171 lines (140 loc) · 4.96 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// AppDelegate.swift
// Disto for Mac
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, BluetoothCommunicationDelegate {
var communication: BluetoothCommunication!
var pasteMode = PasteMode.paste
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var connect: NSMenuItem!
@IBOutlet weak var pasteMatchStyleMenuItem: NSMenuItem!
@IBOutlet weak var pasteMenuItem: NSMenuItem!
@IBAction func connect(_ sender: Any) {
communication.startScanning()
}
@IBAction func quitApp(_ sender: Any) {
NSApplication.shared.terminate(self)
}
@IBAction func setPasteModeToPasteMatchStyle(_ sender: Any) {
pasteMode = .pasteMatchStyle
updateView()
savePasteModeSelection(mode: pasteMode)
}
@IBAction func setPasteModeToPaste(_ sender: Any) {
pasteMode = .paste
updateView()
savePasteModeSelection(mode: pasteMode)
}
@IBAction func measureMenuItem(_ sender: Any) {
communication.writeByte(data: 0x67) //Initiate new measurement
}
func bluetoothDidUpdateState() {
self.connect.title = "BT off"
self.connect.isEnabled = false
}
func bluetoothIsPoweredOn() {
self.connect.title = "Connect"
self.connect.isEnabled = true
}
func deviceDidConnect() {
self.connect.title = "Connected"
self.connect.isEnabled = false
}
func deviceDidDisconnect() {
self.connect.isEnabled = true
self.connect.title = "Connect"
}
func updateView() {
switch pasteMode {
case .paste: do {
pasteMenuItem.state = .on
pasteMatchStyleMenuItem.state = .off
}
case .pasteMatchStyle: do {
pasteMenuItem.state = .off
pasteMatchStyleMenuItem.state = .on
}
}
}
private func copyToClipBoard(textToCopy: Float) {
let buffer = Int(textToCopy*1000) //convert to mm
let pasteBoard = NSPasteboard.general
pasteBoard.clearContents()
pasteBoard.setString(String(buffer), forType: .string)
print("CopyToClipBoard")
}
// Run AppleScript function for pasting
func runScript(name: String) {
if let scriptPath = Bundle.main.url(forResource: name, withExtension: nil)?.path {
let process = Process()
if process.isRunning == false {
let pipe = Pipe()
process.launchPath = "/usr/bin/osascript"
process.arguments = [scriptPath]
process.standardError = pipe
process.launch()
}
} else {
print("Script error")
}
}
func pasteMatchStyle() {
runScript(name: "pasteAndMatchStyle.scpt")
print("pasteAndMatchStyle")
}
func paste() {
runScript(name: "paste.scpt")
print("paste")
}
// Needed to allow a script to emulate user input
func checkAndAskForAssistiveAccess() {
// https://stackoverflow.com/questions/58675555/how-to-grant-accessibilty-access-in-xcode
//get the value for accesibility
let checkOptPrompt = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as NSString
//set the options: false means it wont ask
//true means it will popup and ask
let options = [checkOptPrompt: true]
//translate into boolean value
let accessEnabled = AXIsProcessTrustedWithOptions(options as CFDictionary?)
if accessEnabled == true {
print("Access Granted")
} else {
print("Access not allowed")
}
}
func deviceHasNewData(_ data: Float) {
copyToClipBoard(textToCopy: data)
//Check how to paste the clipboard
switch pasteMode {
case .pasteMatchStyle: pasteMatchStyle()
default: paste()
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
// Set menu bar icon
statusItem.menu = statusMenu
statusMenu.autoenablesItems = false
if let button = statusItem.button {
button.image = #imageLiteral(resourceName: "laser_symbol")
//button.title = "LDM"
}
communication = BluetoothCommunication()
communication.start(withDelegate: self)
//Auto connect on start
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {
self.communication.startScanning()
print("Auto connect")
})
//Check if accessibility access is allowed
checkAndAskForAssistiveAccess()
//Load last paste mode
pasteMode = loadPasteModeFromUserDefaults()
updateView()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}