Skip to content

Instantly share code, notes, and snippets.

@microtherion
Created April 6, 2025 14:39
Show Gist options
  • Save microtherion/1c2e687b30127fb5010ca15809c0757a to your computer and use it in GitHub Desktop.
Save microtherion/1c2e687b30127fb5010ca15809c0757a to your computer and use it in GitHub Desktop.
Get a file URL from a FileWrapper
// Horrifying idea, but it seems to work on Apple platforms
// This is NOT stable across mounting/unmounting a file system
extension FileWrapper {
var fileURL: URL? {
let attr = self.fileAttributes
guard let fileSystemNumber = attr["NSFileSystemNumber"] as? Int,
let fileSystemFileNumber = attr["NSFileSystemFileNumber"] as? Int
else { return nil }
return URL(string: "file:///.file/id=\(fileSystemNumber).\(fileSystemFileNumber)/")
}
}
@microtherion
Copy link
Author

Does not work on macOS, as it turns out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment