-
Notifications
You must be signed in to change notification settings - Fork 59
/
ClipboardManager.swift
49 lines (38 loc) · 1.92 KB
/
ClipboardManager.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
import AppKit
class ClipboardManager {
static let shared = ClipboardManager()
private var changeCount: Int
private var pasteboard: NSPasteboard
init() {
self.pasteboard = NSPasteboard.general
self.changeCount = pasteboard.changeCount
}
func getClipboardIfChanged() -> String? {
// Check if the clipboard has changed since the last check
if pasteboard.changeCount != changeCount {
var newItems: [String] = []
let currentChangeCount = pasteboard.changeCount
// Iterate over the changes since the last recorded changeCount
for _ in changeCount..<currentChangeCount {
if let string = pasteboard.string(forType: .string) {
newItems.append(string)
}
}
// Update the changeCount to the current changeCount
changeCount = currentChangeCount
// Return the concatenated string if there are new items
return newItems.isEmpty ? nil : newItems.joined(separator: "\n")
}
// Return nil if there are no new changes
return nil
}
func replaceClipboardContents(with string: String) {
pasteboard.clearContents()
let finalContents = string.isEmpty ? "No context. Is remembering disabled?" : """
Below is the text that's been on my screen recently. ------------- \(string) ------------------ Above is the text that's been on my screen recently. Please answer whatever I ask using the provided information about what has been on the screen recently. Do not say anything else or give any other information. Only answer the query. --------------------------\n
"""
pasteboard.setString(finalContents, forType: .string)
// We don't want to pickup our own changes
changeCount = pasteboard.changeCount
}
}