Professional Web Applications Themes

OS X 10.3 file extensions/mime-types with LaunchServices - Mac Programming

Our unix mail app ports cleanly over to OS X, but we are prevented from doing proper attachment handling, because unlike most unix flavors, there are no default mailcap or mime.types files, the *nix way of attributing file associations for determing which program to open a file of a specific type. For OS X, I've stumbled upon the LaunchServices API, the blessed way to open files. This does most of what I want, and I was pleased to see in 10.3 a way to translate from mime-type to file extension and vice versa through mechanisms set forth in header file ...

  1. #1

    Default OS X 10.3 file extensions/mime-types with LaunchServices

    Our unix mail app ports cleanly over to OS X, but we are prevented from
    doing proper attachment handling, because unlike most unix flavors, there
    are no default mailcap or mime.types files, the *nix way of attributing
    file associations for determing which program to open a file of a specific
    type.

    For OS X, I've stumbled upon the LaunchServices API, the blessed way to
    open files. This does most of what I want, and I was pleased to see in
    10.3 a way to translate from mime-type to file extension and vice versa
    through mechanisms set forth in header file UTType.h. I'm aware of the
    "open" command, which would work fine if we didn't care about opening
    virus attachments.

    I am starting to draw the conclusion that the LaunchServices API is only a
    half-baked interface, despite being touted by Apple as the way things
    should be done. It finds the correct application for a given MIME-type,
    but as for translating between MIME-type and file extension, I have yet to
    be successful with that; it behaves as though there were no such mappings.

    Some specifics: I've been testing with "image/jpeg" and "image/gif" using
    the routines UTTypeCreatePreferredIdentifierForTag and
    UTTypeCopyPreferredTagWithClass. For the jpeg case it found identifier
    public.jpeg, but the second call was unable to return a file extension.
    For the gif case it returned a dynamic identifier, its way of saying that
    there was no mapping. I can post code samples if it will help, but I'm
    feeling I'm already in over my head.

    Is there anyone else currently struggling with this? How does the Finder
    get at file extension mappings, and how can I get at these mappings? For
    instance, how does "open" know that Preview.app is the way to open a .gif
    file? Is there some database somewhere on the system that stores these
    mappings? If so, where is it? I know better than to wish for something
    like the Windows Registry, but is this the alternative?

    TIA. Let me know if there is a better forum to post this to.

    Jeff
    Jeff Guest

  2. #2

    Default Re: OS X 10.3 file extensions/mime-types with LaunchServices

    In article
    <cac.washington.edu>,
    Jeff Franklin <washington.edu> wrote:
     

    Launch Services does what it's designed to do quite well, but I don't
    think it's designed to provide a mapping between MIME types and file
    extensions. Apple could probably add this in the future, but it's
    unlikely to happen unless people request it. I'd suggest filing a bug
    report at <http://bugreport.apple.com/> explaining why you'd find this
    useful.
     

    The Finder doesn't do MIME type mappings; it just uses extensions and
    type/creator codes. You can map from those to applications via
    LSGetApplicationForInfo in LSInfo.h. If you want to find the
    application for a MIME type instead, use LSCopyApplicationForMIMEType,
    also in LSInfo.h.

    Hope this helps,
    Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  3. #3

    Default Re: OS X 10.3 file extensions/mime-types with LaunchServices

    On Mon, 8 Dec 2003, Eric Albert wrote:
     

    Thanks, will take your advice and file a bug report with apple. I've been
    able to get much of Launch Services working, but the part that seems
    incomplete is the new UTType.h stuff that got added to LaunchServices in
    10.3, which as far as I can provides the way to map between MIME type and
    file extension. The design seems adequate, but whatever database it's
    getting at just doesn't seem to be seeded with the right information, as
    there is not even enough information for Preview.app.
     

    In this case I was curious as to how Finder maps file extensions to their
    applications. Is there collection of files somewhere in the file system
    that it reads to determine these mappings? UTType.h refers to an
    Info.plist for importing new mappings, but I was wondering if there was
    some repository for these things that the Finder uses.

    Also, I have successfully used LSCopyApplicationForMIMEType, but it seems
    that programs require a file extension to open a file (this is at least
    true for Preview.app and a gif file), hence my problem. I think I have
    come up with a temporary hack to work around this particular problem, but
    still need the mappings to correctly label outgoing attachments.

    Again, thanks for your help,
    Jeff
    Jeff Guest

  4. #4

    Default Re: OS X 10.3 file extensions/mime-types with LaunchServices

    In article
    <cac.washington.edu>,
    Jeff Franklin <washington.edu> wrote:
     
    >
    > Thanks, will take your advice and file a bug report with apple. I've been
    > able to get much of Launch Services working, but the part that seems
    > incomplete is the new UTType.h stuff that got added to LaunchServices in
    > 10.3, which as far as I can provides the way to map between MIME type and
    > file extension. The design seems adequate, but whatever database it's
    > getting at just doesn't seem to be seeded with the right information, as
    > there is not even enough information for Preview.app.[/ref]

    I'm not sure if there's a way for applications to specify that they
    understand MIME types. [Actually, I stand corrected after reading
    UTType.h -- as of Panther, applications can declare that they understand
    specific MIME types. Nifty. I'd love to see some official
    doentation from Apple about this.]

    That said, I can't think of an API that would send a MIME type along
    with a file to an application, or one that'd allow an app to request the
    MIME type for a file that doesn't have an extension. Do other operating
    systems provide APIs to do that? I'm not sure where the data would be
    stored unless they had a metadata-friendly file system (as few systems
    today do).
     
    >
    > In this case I was curious as to how Finder maps file extensions to their
    > applications. Is there collection of files somewhere in the file system
    > that it reads to determine these mappings? UTType.h refers to an
    > Info.plist for importing new mappings, but I was wondering if there was
    > some repository for these things that the Finder uses.[/ref]

    I'll correct your terminology a bit -- the Finder calls the same Launch
    Services APIs as you're looking at, so the repository in question
    belongs to Launch Services, not to the Finder.

    Launch Services (with the help of a daemon and some other means) scans
    applications for their supported file bindings. Those are generally
    stored in the Info.plist file in an application bundle, but they can
    also be stored in a 'plst' resource for old-style non-bundled
    applications.
     

    The problem that you're facing is basically what I explained above.
    When you try to open the file in Preview, Preview doesn't receive any
    MIME type information. It just gets the path to the file. It can then
    ask the file system for any metadata that's available, such as a type or
    creator code, or a MIME type generated from the file's extension, but it
    doesn't have any way to get a non-generated MIME type for the file.
    Preview therefore doesn't have any way to know that your file is a GIF
    (for example). It could sniff the file's contents, but unfortunately it
    isn't smart enough to do so.

    After glancing at UTType.h, though, it looks like Apple should be able
    to provide a means to associate a MIME type with a file that is then
    opened in another application, and a way for that application to
    retrieve the file's suggested MIME type. That'd be another good bug to
    file. :) If you do file it, be sure to explain why you need a way to do
    that, since that'll make it easier for their engineers to come up with a
    solution that works well for you.

    -Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  5. #5

    Default Re: OS X 10.3 file extensions/mime-types with LaunchServices

    On Tue, 9 Dec 2003, Eric Albert wrote:
     

    I've checked in with the carbon-development list over at apple, where a
    developer at apple relayed the information that the database was "not yet
    ready for general use as a tag converter" and that it was brought about
    "to support the Carbon Pasteboard and Translation Services APIs". That
    said, I have now successfully been able to translate between image/jpeg
    and .jpg, as well as text/html. There just isn't an entry for image/gif.
    My own speculation is that UTType is a work in progress that they aren't
    ready to doent in case they want to go some other direction.
     

    I know of no API where you would launch by passing the filename and
    mime-type, and it really isn't necessary. What we try to do instead is
    save the file to disk, launching the program that associates with the
    provided mime-type, but as I discovered for Preview.app, it relies on the
    file's extension. Hence, I need to translate. I think I've reached a
    mostly reliable way to do that, but it'd be nice for UTType to fall into
    place to allow for sending attachments (the other half of the problem) to
    be correctly labeled. I see that a lot of OS X apps' Info.plist's do have
    their own mime-type declarations, so it at least looks we're approaching
    something workable.

    On Windows, there's the registry, which certainly has its own problems,
    but on an uncorrupted system has a pretty reliable way of mapping between
    extension and mime-type.
     

    Thanks, it's starting to come together for me.
     

    From what I see in UTType.h, they're mostly there, they just need to
    populate their database, and make it easy for other apps to do the same,
    and maybe that's what Launch Services will do with an app's Info.plist in
    the near future. Being able to pass a filename and one of these UTI types
    to the launcher might help, but I guess the answer for today would likely
    be that the file extension currently serves as a "Universal Type
    Identifier" on most OSes.

    Thanks for the help,
    Jeff
    Jeff Guest

Similar Threads

  1. APACHE2 Mime Types Config
    By reya276 in forum Coldfusion Server Administration
    Replies: 2
    Last Post: May 8th, 01:08 PM
  2. Setting MIME Types
    By MartinThorpe in forum Coldfusion Server Administration
    Replies: 0
    Last Post: January 4th, 11:43 AM
  3. MIME::Types 1.16 and OpenDoent
    By lars@nospam.nosoftwarepatents.edu in forum PERL Modules
    Replies: 0
    Last Post: November 9th, 07:48 AM
  4. [ANN] Text::Format 0.64, MIME::Types 1.07
    By Austin Ziegler in forum Ruby
    Replies: 1
    Last Post: September 18th, 02:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139