For some reason the UTIDescription I get through my Swift project differs from Finder's 'Get Info` file kind field for a number of files.
For example below you can see on the left, the UTIDescription I get is quite lengthy and the on the right provided by Finder is short and makes sense ⤵︎
Below is the code I use to get the UTIDescription of a targeted file
...
// Grab the extension and unique type identifier
itemExtension = NSURL(fileURLWithPath: path!).pathExtension!
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension as CFString, itemExtension as CFString, nil)?.takeRetainedValue()
// Determines if the uti conforms
func doesConform(kUTType: CFString) -> Bool {
return UTTypeConformsTo(uti!, kUTType)
}
// Gets the item's UTI description
func getUTIDescription() -> String? {
if UTTypeCopyDescription(uti!)?.takeRetainedValue() != nil {
return UTTypeCopyDescription(uti!)!.takeRetainedValue() as String
} else {
return nil
}
}
.
.
.
Is the conflict just a result from custom UTIDescription's in Finder or am I grabbing the wrong property here?
Post
Replies
Boosts
Views
Activity
Problem
When grabbing the .fileSizeKey and .localizedTypeDescriptionKey on a URL.resourceValues's for an non-downloaded iCloud file with the .icloud extension, I get an incorrect file size and the description is listed as Alias.
var url = URL(fileURLWithPath: "/.image.png.icloud")
var fileResources: URLResourceValues?
let keys: Set<URLResourceKey> = [.localizedTypeDescriptionKey, .fileSizeKey]
do {
guard let resources = try url?.resourceValues(forKeys: keys) else { return }
fileResources = resources
} catch {}
print(fileResources?.localizedTypeDescription) // Alias
print(fileResources?.fileSize) // 192 bytes (when it should be 4MB)
.
What I've Tried
I've tried setting up a NSFileCoordinator() to request the metadata for the file but that didn't work.
var error: NSError?
let coordinator = NSFileCoordinator(filePresenter: nil)
coordinator.coordinate(readingItemAt: url!, options: .immediatelyAvailableMetadataOnly, error: &error) { URL in
do {
let resources = try URL.promisedItemResourceValues(forKeys: [.fileSizeKey])
print(resources.fileSize) // 192 bytes (file type reads as Alias too)
} catch {}
}
I've also tried MDItem and it does work, but it's unreliable because sometimes there's no metadata (on mail files for example) and I had to turn off sandboxing to get it to work.
let md = MDItemCreateWithURL(kCFAllocatorDefault, url! as CFURL) // nil if sandbox is enabled
let meta = MDItemCopyAttribute(md, kMDItemFSSize)
print(meta) // 4MB (correct size)
I've also tried taking the .image.png.icloud url and tried to find the alias original url, but it doesn't seem possible as the file isn't actually an alias.
.
What I'm Considering
I imagine this is possible somehow as Finder's Get Info Pane has no problem getting the info. I've wondered if maybe it's possible to change the file and remove the '.' and the '.icloud' components of the url temporarily but I'm not sure how to go about this because I believe it involves duplication or copying of the file.
Thank you in advance! I posted this on Stack Overflow as well