ghostty/macos/Sources/Helpers/NSPasteboard+Extension.swift
Qwerasd d1ac0aff39 feat(macOS): Paste copied files as absolute paths.
Previously files would be pasted as only the filename. This commit
introduces an extension to NSPasteboard which provides a method to
consistently get the string contents of a pasteboard so that the
behavior can stay the same anywhere where we need to do that.
2024-03-07 20:53:48 -05:00

19 lines
664 B
Swift

import AppKit
extension NSPasteboard {
/// Gets the contents of the pasteboard as a string following a specific set of semantics.
/// Does these things in order:
/// - Tries to get the absolute filesystem path of the file in the pasteboard if there is one.
/// - Tries to get any string from the pasteboard.
/// If all of the above fail, returns None.
func getOpinionatedStringContents() -> String? {
let file = self.string(forType: .fileURL)
if let file = file {
if let path = NSURL(string: file)?.path {
return path
}
}
return self.string(forType: .string)
}
}