-
Notifications
You must be signed in to change notification settings - Fork 18
/
AirDropCLI.swift
122 lines (98 loc) · 3.82 KB
/
AirDropCLI.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
//
// AirDropCLI.swift
// airdrop
//
// Created by Volodymyr Klymenko on 2020-12-30.
//
import Foundation
import Cocoa
enum OptionType: String {
case help = "h"
case unknown
init(value: String) {
switch value {
case "-h", "--help": self = .help
default: self = .unknown
}
}
}
class AirDropCLI: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {
let consoleIO = ConsoleIO()
func applicationDidFinishLaunching(_ aNotification: Notification) {
let argCount = Int(CommandLine.argc)
guard argCount >= 2 else {
consoleIO.printUsage()
exit(0)
}
let argument = CommandLine.arguments[1]
if argCount == 2 && argument.hasPrefix("-") {
let (option, _) = getOption(argument)
if option == .help {
consoleIO.printUsage()
} else {
consoleIO.writeMessage("Unknown option, see usage.\n", to: .error)
consoleIO.printUsage()
}
exit(0)
}
let pathsToFiles = Array(CommandLine.arguments[1 ..< argCount])
shareFiles(pathsToFiles)
if #available(macOS 13.0, *) {
NSApp.setActivationPolicy(.accessory)
}
}
func getOption(_ option: String) -> (option:OptionType, value: String) {
return (OptionType(value: option), option)
}
func shareFiles(_ pathsToFiles: [String]) {
guard let service: NSSharingService = NSSharingService(named: .sendViaAirDrop)
else {
exit(2)
}
var filesToShare: [URL] = []
for pathToFile in pathsToFiles {
if let url = URL(string: pathToFile), url.scheme != nil {
filesToShare.append(url)
} else {
let fileURL: URL = NSURL.fileURL(withPath: pathToFile, isDirectory: false)
filesToShare.append(fileURL.standardizedFileURL)
}
}
if service.canPerform(withItems: filesToShare) {
service.delegate = self
service.perform(withItems: filesToShare)
} else {
consoleIO.writeMessage("Can't perform: file is likely to be nonexistent.", to: .error)
exit(1)
}
}
func sharingService(_ sharingService: NSSharingService, willShareItems items: [Any]) {
consoleIO.writeMessage("Sending \(items.count) files:")
for item in items {
consoleIO.writeMessage("📩 \(item)")
}
}
func sharingService(_ sharingService: NSSharingService, didShareItems items: [Any]) {
consoleIO.writeMessage("✅ Files were sent successfully!")
exit(0)
}
func sharingService(_ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error) {
consoleIO.writeMessage(error.localizedDescription, to: .error)
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 airDropMenuWindow = NSWindow(contentRect: .init(origin: .zero,
size: .init(width: 1,
height: 1)),
styleMask: [.closable],
backing: .buffered,
defer: false)
airDropMenuWindow.center()
airDropMenuWindow.level = .popUpMenu
airDropMenuWindow.makeKeyAndOrderFront(nil)
return airDropMenuWindow
}
}