Dan Wood: The Eponymous Weblog (Archives)

Dan Wood Dan Wood is co-owner of Karelia Software, creating programs for the Macintosh computer. He is the father of two kids, lives in the Bay Area of California USA, and prefers bicycles to cars. This site is his older weblog, which mostly covers geeky topics like Macs and Mac Programming. Go visit the current blog here.

Useful Tidbits and Egotistical Musings from Dan Wood

Categories: Business · Mac OS X · Cocoa Programming · General · All Categories

Thu, 17 May 2007

Adding support for new file types in your application

Recently somebody asked us why Sandvox didn't have support for MIDI files. I tried it out and sure enough, even though Quicktime knows how to play a MIDI file, I couldn't import one into Sandvox.

It turns out that MIDI files are not listed as one of the UTIs (uniform type identifiers) built into Mac OS X.

Sandvox uses UTIs to match file types, as much as possible (since there are some methods in Cocoa that still — as of Tiger — don't have UTI equivalents). So I was able to fix this problem without actually changing any code!


What I did was to add my own declaration of a UTI for MIDI files to the application's Info.plist file. But what is the right UTI to use? Well, you can make one up of course, but I decided to use a nice third-party list that is a combination of official and unofficial UTIs. In this case, I chose "public.midi".

The way to declare a new UTI is to declare an array of specific dictionaries under the key of UTImportedTypeDeclarations, and fill in the keys/values as much as possible. Here is what it looks like in our info.plist:

<key>UTImportedTypeDeclarations</key>
<array>
  <dict>
    <key>UTTypeDescription</key>
    <string>MIDI</string>
    <key>UTTypeIdentifier</key>
    <string>public.midi</string>
    <key>UTTypeConformsTo</key>
    <array>
      <string>public.audio</string>
      <string>public.audiovisual-content</string>
      <string>public.data</string>
      <string>public.item</string>
    </array>
    <key>UTTypeReferenceURL</key>
    <string>http://www.midi.org/about-midi/specinfo.shtml</string>
    <key>UTTypeTagSpecification</key>
    <dict>
      <key>public.filename-extension</key>
      <array>
        <string>mid</string>
        <string>midi</string>
      </array>
      <key>public.mime-type</key>
      <string>audio/midi</string>
    </dict>
  </dict>
</array>

We have also found the need to include declarations for Flash files ("com.macromedia.shockwave-flash") and cascading style sheet files ("public.css").

Hopefully Apple will continue to expand its list of built-in UTIs so that this is not necessary, except for obscure cases.