1import Cocoa
2
3// Compilation: swiftc file_picker.swift -o file_picker
4// Usage: ./file_picker [initial_path]
5
6func openFilePicker(initialPath: String?) {
7 let dialog = NSOpenPanel()
8
9 dialog.title = "Select an attachment"
10 dialog.showsResizeIndicator = true
11 dialog.showsHiddenFiles = false
12 dialog.canChooseDirectories = false
13 dialog.canCreateDirectories = false
14 dialog.allowsMultipleSelection = true
15
16 if let initialPath = initialPath {
17 dialog.directoryURL = URL(fileURLWithPath: (initialPath as NSString).expandingTildeInPath)
18 }
19
20 // Since this is a CLI helper, we need to force it to the front
21 NSApp.setActivationPolicy(.accessory)
22 NSApp.activate(ignoringOtherApps: true)
23
24 if dialog.runModal() == .OK {
25 let results = dialog.urls.map { $0.path }
26 print(results.joined(separator: "\n"))
27 }
28}
29
30let args = ProcessInfo.processInfo.arguments
31let initialPath = args.count > 1 ? args[1] : nil
32
33openFilePicker(initialPath: initialPath)