-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAirDropBridge.swift
More file actions
71 lines (58 loc) · 2.54 KB
/
AirDropBridge.swift
File metadata and controls
71 lines (58 loc) · 2.54 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
import Foundation
import Cocoa
@_cdecl("ShareViaAirDrop")
public func ShareViaAirDrop(_ cPaths: UnsafePointer<UnsafePointer<CChar>?>, _ count: Int32) -> Int32 {
let args = UnsafeBufferPointer(start: cPaths, count: Int(count))
let paths: [String] = args.compactMap { $0.flatMap { String(cString: $0) } }
let urls = paths.map { URL(fileURLWithPath: $0) }
print("🧾 Files to send via AirDrop:")
for url in urls {
print(" - \(url.path)")
}
guard let service = NSSharingService(named: .sendViaAirDrop) else {
print("❌ Failed to get AirDrop service")
return 1
}
if !service.canPerform(withItems: urls) {
print("❌ AirDrop cannot perform with given items.")
return 1
}
class AirDropDelegate: NSObject, NSSharingServiceDelegate {
func sharingService(_ sharingService: NSSharingService, willShareItems items: [Any]) {
print("📤 Preparing to share \(items.count) item(s)")
}
func sharingService(_ sharingService: NSSharingService, didShareItems items: [Any]) {
print("✅ Shared successfully")
exit(0)
}
func sharingService(_ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error) {
print("❌ Failed to share: \(error.localizedDescription)")
exit(1)
}
func sharingService(_ sharingService: NSSharingService, sourceFrameOnScreenForShareItem item: Any) -> NSRect {
return NSRect(x: 0, y: 0, width: 400, height: 100)
}
func sharingService(_ sharingService: NSSharingService, sourceWindowForShareItems items: [Any], sharingContentScope: UnsafeMutablePointer<NSSharingService.SharingContentScope>) -> NSWindow? {
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1, height: 1),
styleMask: [],
backing: .buffered,
defer: false)
window.level = .floating
window.center()
window.orderFrontRegardless()
return window
}
}
let delegate = AirDropDelegate()
service.delegate = delegate
// Ensure app is running with full UI access
let app = NSApplication.shared
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps: true)
service.perform(withItems: urls)
// Run event loop to allow AirDrop UI interaction
let runLoop = RunLoop.current
while true {
runLoop.run(until: Date(timeIntervalSinceNow: 0.1))
}
}