34

Is there a command line utility to which I can pass a filename, that will return applicable UTIs for that file?

For example:

hypothetical-uti-fetcher /Library/Desktop\ Pictures/Aqua\ Blue.jpg

might give me:

public.jpeg
public.image
public.data

4 Answers 4

41

You can use the mdls command to find the UTI for a file:

mdls /Library/Desktop\ Pictures/Aqua\ Blue.jpg

and it will give you a bunch of info, toward the top it will say something like

kMDItemContentType             = "public.plain-text"

Hope this helps!

5
  • 4
    Excellent. Thanks. mdls -name kMDItemContentTypeTree /Library/Desktop\ Pictures/Aqua\ Blue.jpg is exactly what I wanted.
    – smokris
    Nov 10, 2010 at 0:41
  • You're welcome, I'm glad this helped.
    – Wuffers
    Nov 10, 2010 at 1:06
  • 2
    Suppose I turned off my Spotlight index and I seem to have no metadata stored. (mdls whatever-file just says that the file was not found). How do the md* services generate the content types in the first place? Feb 17, 2015 at 17:53
  • Even more concise, would be mdls -name kMDItemContentType <filename> | awk -F\" '{ print $2 }'
    – luckman212
    Aug 20, 2020 at 16:18
  • Even more concise, would be mdls -name kMDItemContentType <filename> Apr 5, 2021 at 13:15
8

The mdls command only seems to work on Spotlight indexed files. If you run it on a mounted disk image, cd or thumb drive it may return no information at all.

When you're developing a Quicklook Plugin it's also useful to use the following command to see what UTIs Quicklook thinks a file has and which plugins it uses for the preview:

qlmanage -d 4 -p /path/to/file
1
  • The cited site is no longer available.
    – Randall
    Jan 15, 2019 at 10:36
1

Even if Spotlight hasn't indexed the file you can still use mdls to find the UTI using:

mdls -name kMDItemContentType /path/to/file

0

You get create a workflow for getting the UTI from selected file.

on run {input, parameters}
    tell application "System Events"
        set ff to item 1 of input
        set uti to type identifier of ff
        set ans to display dialog uti buttons {"Copy to Clipboard", "OK"} default button "Copy to Clipboard"
        if button returned of ans is "Copy to Clipboard" then
            set the clipboard to uti
        end if
    end tell
end run

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .