forked from zeitlings/alfred-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQResolve.swift
executable file
·59 lines (52 loc) · 1.32 KB
/
QResolve.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
#!/usr/bin/swift
//
// QResolve.swift
// Resolve and open URLs from QR codes
//
// Created by Patrick Sy on 08/05/2023.
//
import CoreImage
let qrPath: String = "/tmp/qrsnap.png"
struct QResolver {
static let fileManager: FileManager = .default
static func run() {
if let snap: CIImage = fileManager.getSnap(),
let detector: CIDetector = .quickResponse()
{
fileManager.removeSnap()
for feature in detector.features(in: snap) where feature is CIQRCodeFeature {
guard let feature = feature as? CIQRCodeFeature,
let landingPage: String = feature.messageString
else {
preconditionFailure()
}
print(landingPage, terminator: "")
exit(0)
}
}
fileManager.removeSnap()
print("Failure: No valid QR code detected")
}
}
extension FileManager {
@discardableResult
func getSnap() -> CIImage? {
guard fileExists(atPath: qrPath) else {
if let received: String = ProcessInfo.processInfo.environment["qr"] {
return CIImage(contentsOf: URL(filePath: received))
}
print("Failure: Nothing to recognize")
exit(1)
}
return CIImage(contentsOf: URL(filePath: qrPath))
}
func removeSnap() {
try? removeItem(atPath: qrPath)
}
}
extension CIDetector {
static func quickResponse() -> CIDetector? {
.init(ofType: CIDetectorTypeQRCode, context: nil, options: nil)
}
}
QResolver.run()