In PyQt4 on Apple Mac OS X, we get a file reference URL to a drag-and-dropped file in the following format:
file:///.file/id=123456.78901
This, interestingly, is not a valid file URL, or at least it doesn't transparently resolve in most applications — there is no /.file/id=...
file in the filesystem.
Using just shell, how do I dereference this NSURL or file reference or alias (or whatever it's called) into a valid path to file on the filesystem?
In Swift you can use URL.standardized to get the valid path from a reference URL. So in Swift 4 - to get the file name from a drag-and-dropped file you'd do something like...
let fileType = NSPasteboard.PasteboardType (kUTTypeFileURL as String)
extension NSPasteboardItem {
func fileURL ()->URL? {
if let refURL = self.string(forType: fileType) {
return URL (fileURLWithPath: refURL).standardized
}
return nil
}
}
One can use osascript
, an AppleScript interpreter available on default OS X installs, run with the following script:
osascript -e 'get posix path of posix file "file:///.file/id=123.456" -- kthxbai'
Prints /Users/josh/Downloads/paste-10837081.py
.
That is a NSURL object, to get a path from it:
NSString *filePath = [fileURL path];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With