41 lines
1.2 KiB
Swift
Executable File
41 lines
1.2 KiB
Swift
Executable File
#!/usr/bin/env swift
|
|
|
|
import AppKit
|
|
import Foundation
|
|
|
|
let paths = CommandLine.arguments.dropFirst()
|
|
|
|
guard !paths.isEmpty else {
|
|
fputs("usage: copy_files_to_clipboard.swift <path> [path...]\n", stderr)
|
|
exit(64)
|
|
}
|
|
|
|
let urls = paths.map { URL(fileURLWithPath: $0).standardizedFileURL }
|
|
let missing = urls.filter { !FileManager.default.fileExists(atPath: $0.path) }
|
|
|
|
guard missing.isEmpty else {
|
|
for url in missing {
|
|
fputs("missing file: \(url.path)\n", stderr)
|
|
}
|
|
exit(66)
|
|
}
|
|
|
|
let pasteboard = NSPasteboard.general
|
|
pasteboard.clearContents()
|
|
|
|
let wroteObjects = pasteboard.writeObjects(urls as [NSURL])
|
|
|
|
let filenamesType = NSPasteboard.PasteboardType("NSFilenamesPboardType")
|
|
let pathsList = urls.map(\.path)
|
|
let wroteFilenames = pasteboard.setPropertyList(pathsList, forType: filenamesType)
|
|
let wrotePlainText = pasteboard.setString(pathsList.joined(separator: "\n"), forType: .string)
|
|
|
|
if wroteObjects || wroteFilenames || wrotePlainText {
|
|
let itemCount = pasteboard.pasteboardItems?.count ?? 0
|
|
fputs("pasteboard files=\(urls.count) items=\(itemCount) objects=\(wroteObjects) filenames=\(wroteFilenames) text=\(wrotePlainText)\n", stderr)
|
|
exit(0)
|
|
}
|
|
|
|
fputs("failed to write file URLs to pasteboard\n", stderr)
|
|
exit(1)
|