Get the path from the alias of the URL of the link to the OS X file (file: ///. File / id = ...)

In PyQt4 on Apple Mac OS X, we get a file link for the dragged file in the following format:

file:///.file/id=123456.78901

This, interestingly, is not a valid file URL, or at least it is not transparently allowed in most applications - there is no file in the file system /.file/id=....

Using only the shell, how do I dereference this NSURL link or file or alias (or something else) so that it allows a file path on the file system?

+4
source share
3 answers

osascript, AppleScript , OS X, script:

osascript -e 'get posix path of posix file "file:///.file/id=123.456" -- kthxbai'

/Users/josh/Downloads/paste-10837081.py.

+1

, " ", PyObjC: Foundation.NSURL.alloc().initWithString_(url).fileSystemRepresentation()
file:///.file/id=123456.78901
b'/Users/dave/Desktop/testbox/dest'

0

In Swift, you can use the URL standard to get a valid path from the reference URL. So, in Swift 4 - to get the file name from a drag-and-drop file, you would 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
    }
}
0
source

All Articles