Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path from OS X file reference URL alias (file:///.file/id=...)

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?

like image 345
K3---rnc Avatar asked May 20 '16 16:05

K3---rnc


3 Answers

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
    }
}
like image 133
Colin Wilson Avatar answered Nov 16 '22 02:11

Colin Wilson


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.

like image 45
K3---rnc Avatar answered Nov 16 '22 04:11

K3---rnc


That is a NSURL object, to get a path from it:

NSString *filePath = [fileURL path];
like image 36
Siyu Avatar answered Nov 16 '22 04:11

Siyu