Getting file kind as shown in there Finder?

Hi, how can I obtain the file kind info as shown in the ‘Kind’ column in the Finder?
Anyone? Thanks!

Maybe there’s an “official” way, but you could always just:

  1. split the filename to get the extension

  2. create a small method to map the extension onto a longer more informative string of your choice

Hi Tim, the method is good but it does not supply the Kind information, I have to create it and map it myself. I don’t have alle the mappings. Finder has, but how to get it?

I just did a little experiment. It looks like all that Finder does, is what I suggested. I changed a .jpeg to be a .docx, and Finder naively changed the Kind info to match.

Let’s say I have a PNG file called image.png.
Finder info will show it as PNG file…

So now I rename the file to just image without an extension…and Finder tells me that this is just a document…

But let’s try this on the shell:

file image.png 
image.png: PNG image data, 24 x 24, 8-bit/color RGBA, non-interlaced

and then rename it to just image:

file image
image: PNG image data, 24 x 24, 8-bit/color RGBA, non-interlaced

Doesn’t work for all file types…you just have to test yourself…for example an Excel file gives me:

file document.xls 
document.xls: Composite Document File V2 Document, Little Endian, Os: MacOS, Version 10.3, Code page: 10000, Author: SOmeone, Last Saved By: Me, Name of Creating Application: Microsoft Macintosh Excel, Last Printed: Sat Oct 18 06:11:48 2014, Create Time/Date: Sun Oct 12 06:20:19 2014, Last Saved Time/Date: Sat Oct 18 06:26:45 2014, Security: 0

or shorter:

file -I document.xls
document.xls: application/vnd.ms-excel; charset=binary

Now we rename a Word document to a spreadsheet and check its file type after:

mv 942.docx 942.xls 
file 942.xls 
942.xls: Microsoft Word 2007+

So going by the extension .xls this would have failed.

Well Finder doesn’t seem to bother with all this. My view is that if we’re all so simple-minded as to accept that having meta-data about a file as part of the filename (instead of being in the directory entry where it belongs), then we deserve any attendant problems.

What do you want to do ?
a.k.a. why do you want that information ?

: is not allowed in file names on macOS. It is macOS 9’s folder separator.
/ is not allowed in file names on macOS, it is the current macOS folder separator.

You may well be able to add them with terminal but you will end up with no end of troubles.

Did someone mention : and / ? (o;

1 Like

image

As you can see in the screen shot above, I used a ‘/’ in a file name.

But, ‘:’ is rejected by the Finder, changed to ‘-’ in a Save / Save As Dialog.

And, Richard is correct.

As I said: “You may well be able to add them with terminal but you will end up with no end of troubles.”

The following shell command works well
mdls -n "kMDItemKind" filename

1 Like

Hmm…

file image
image: PNG image data, 24 x 24, 8-bit/color RGBA, non-interlaced
mdls -n "kMDItemKind" image   
kMDItemKind = "Document"

…but it seems to exactly do what Finder info thinks (o;

This just looks at the extension, like Finder.

I have a listbox with files and need a column with file Kind, just like the finder.

Look into the Launch Services API. There are system calls that do what you need, and if memory serves, command line tools as well.

2 Likes

Thanks, this works. One more question. Is this shell method allowed for submitting apps to the Mac App Store?