How do I access Finder's 'Get Info' file kinds?

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?

Answered by DTS Engineer in 676746022

Try this:

let u: URL = …
let r = try u.resourceValues(forKeys: [.localizedTypeDescriptionKey])
print(r.localizedTypeDescription!)
// prints: Microsoft Word Document

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Try this:

let u: URL = …
let r = try u.resourceValues(forKeys: [.localizedTypeDescriptionKey])
print(r.localizedTypeDescription!)
// prints: Microsoft Word Document

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

When trying to get the info on an iCloud file that isn’t downloaded

It’s probably best to start a new thread for this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo I'm afraid that doesn't help. Did you mean to grab it some other way? I tested it un-sandboxed too btw and the file kind appears normally but the size is still incorrect. I went into detail on what I've tried on Stack Overflow

var fileResources: URLResourceValues?

let keys: Set<URLResourceKey> = [
	.localizedTypeDescriptionKey,
	.fileSizeKey,
]

DispatchQueue.global(qos: .background).async {
	do {
		guard let resources = try self.url?.resourceValues(forKeys: keys) else { return }
		self.fileResources = resources
	}
	catch {}

	DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
		print(self.fileResources?.localizedTypeDescription)
		print(self.fileResources?.fileSize)
	}
}

Did you mean to grab it some other way?

Sorry about the confusion. I meant “start a new thread here on DevForums”, because you’re question is only tangentially related to _tyirvine’s original question, which they’ve already marked as solved.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How do I access Finder's 'Get Info' file kinds?
 
 
Q