File UTI on 10.6.8

  • Dear list,

    I want to check if a file extension is registered as a text file. So I made a little category method on NSString like this:

    - (BOOL)isText
    {
      BOOL fileIsText = NO;

      CFStringRef fileExtension = (CFStringRef) self;
      CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
      if (UTTypeConformsTo(fileUTI, kUTTypeText)) {
        fileIsText = YES;
      }
      CFRelease(fileUTI);

      return fileIsText;
    }

    This works fine on 10.7. On 10.6.8 when

    self = @"tex"

    this returns NO and fileUTI is dyn.age81k3p2.

    Anybody got an idea what this dyn.* means?

    Cheers,

    Martin
  • On Jan 2, 2012, at 23:57 , Martin Hewitson wrote:

    > I want to check if a file extension is registered as a text file. So I made a little category method on NSString like this:
    >
    > - (BOOL)isText
    > {
    > BOOL fileIsText = NO;
    >
    > CFStringRef fileExtension = (CFStringRef) self;
    > CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
    > if (UTTypeConformsTo(fileUTI, kUTTypeText)) {
    > fileIsText = YES;
    > }
    > CFRelease(fileUTI);
    >
    > return fileIsText;
    > }
    >
    > This works fine on 10.7. On 10.6.8 when
    >
    > self = @"tex"
    >
    > this returns NO and fileUTI is dyn.age81k3p2.
    >
    > Anybody got an idea what this dyn.* means?

    Well, you don't need to ask *us* when the answer in the the documentation for the 'UTTypeCreatePreferredIdentifierForTag' function:

    > "If no result is found, this function creates a dynamic type beginning with the dyn prefix. This allows you to pass the UTI around and convert it back to the original tag."

    OTOH, I doubt that you really want to *create* a UTI just to test conformance, especially since you immediately discard the created UTI. What's wrong with using one of the NSWorkspace methods, such as 'filenameExtension:isValidForType:'?
  • On 3, Jan, 2012, at 09:42 AM, Quincey Morris wrote:

    > On Jan 2, 2012, at 23:57 , Martin Hewitson wrote:
    >
    >> I want to check if a file extension is registered as a text file. So I made a little category method on NSString like this:
    >>
    >> - (BOOL)isText
    >> {
    >> BOOL fileIsText = NO;
    >>
    >> CFStringRef fileExtension = (CFStringRef) self;
    >> CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
    >> if (UTTypeConformsTo(fileUTI, kUTTypeText)) {
    >> fileIsText = YES;
    >> }
    >> CFRelease(fileUTI);
    >>
    >> return fileIsText;
    >> }
    >>
    >> This works fine on 10.7. On 10.6.8 when
    >>
    >> self = @"tex"
    >>
    >> this returns NO and fileUTI is dyn.age81k3p2.
    >>
    >> Anybody got an idea what this dyn.* means?
    >
    > Well, you don't need to ask *us* when the answer in the the documentation for the 'UTTypeCreatePreferredIdentifierForTag' function:
    >
    >> "If no result is found, this function creates a dynamic type beginning with the dyn prefix. This allows you to pass the UTI around and convert it back to the original tag."

    Oops. Completely missed that. Even after reading your post it took me a while to see it. Probably time for another coffee.

    >
    > OTOH, I doubt that you really want to *create* a UTI just to test conformance, especially since you immediately discard the created UTI. What's wrong with using one of the NSWorkspace methods, such as 'filenameExtension:isValidForType:'?

    Thanks! I didn't know about this NSWorkspace method. I think a google search got me to the solution I was using.

    Anyway, thanks, I'll try the NSWorkspace method.

    Cheers,

    Martin

    >
    >

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Martin Hewitson
    Albert-Einstein-Institut
    Max-Planck-Institut fuer
        Gravitationsphysik und Universitaet Hannover
    Callinstr. 38, 30167 Hannover, Germany
    Tel: +49-511-762-17121, Fax: +49-511-762-5861
    E-Mail: <martin.hewitson...>
    WWW: http://www.aei.mpg.de/~hewitson
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • Actually, it seems that the NSWorkspace method behaves differently, (assuming I'm using it correctly):

    [[NSWorkspace sharedWorkspace] filenameExtension:@"pdf" isValidForType:(NSString *)kUTTypeText]

    returns YES

    whereas the UTTypeConformsTo() solution returns NO.

    So am I passing the wrong value for Type?

    Cheers,

    Martin

    On 3, Jan, 2012, at 11:04 AM, Martin Hewitson wrote:

    >
    > On 3, Jan, 2012, at 09:42 AM, Quincey Morris wrote:
    >
    >> On Jan 2, 2012, at 23:57 , Martin Hewitson wrote:
    >>
    >>
    >> Well, you don't need to ask *us* when the answer in the the documentation for the 'UTTypeCreatePreferredIdentifierForTag' function:
    >>
    >>> "If no result is found, this function creates a dynamic type beginning with the dyn prefix. This allows you to pass the UTI around and convert it back to the original tag."
    >
    > Oops. Completely missed that. Even after reading your post it took me a while to see it. Probably time for another coffee.
    >
    >>
    >> OTOH, I doubt that you really want to *create* a UTI just to test conformance, especially since you immediately discard the created UTI. What's wrong with using one of the NSWorkspace methods, such as 'filenameExtension:isValidForType:'?
    >
    > Thanks! I didn't know about this NSWorkspace method. I think a google search got me to the solution I was using.
    >
    > Anyway, thanks, I'll try the NSWorkspace method.
    >
    > Cheers,
    >
    > Martin
    >
    >
    >>
    >>
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > Martin Hewitson
    > Albert-Einstein-Institut
    > Max-Planck-Institut fuer
    > Gravitationsphysik und Universitaet Hannover
    > Callinstr. 38, 30167 Hannover, Germany
    > Tel: +49-511-762-17121, Fax: +49-511-762-5861
    > E-Mail: <martin.hewitson...>
    > WWW: http://www.aei.mpg.de/~hewitson
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    >
    >
    >
    >

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Martin Hewitson
    Albert-Einstein-Institut
    Max-Planck-Institut fuer
        Gravitationsphysik und Universitaet Hannover
    Callinstr. 38, 30167 Hannover, Germany
    Tel: +49-511-762-17121, Fax: +49-511-762-5861
    E-Mail: <martin.hewitson...>
    WWW: http://www.aei.mpg.de/~hewitson
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • On Jan 3, 2012, at 02:11 , Martin Hewitson wrote:

    > Actually, it seems that the NSWorkspace method behaves differently, (assuming I'm using it correctly):
    >
    > [[NSWorkspace sharedWorkspace] filenameExtension:@"pdf" isValidForType:(NSString *)kUTTypeText]
    >
    > returns YES
    >
    > whereas the UTTypeConformsTo() solution returns NO.

    One thing to consider is that your UTType… approach uses the *preferred* UTI for '.pdf', which may indeed not conform to the text type. But there could be another '.pdf'-based UTI defined on your system that does conform. AFAICT the standard UTIs don't contain a text-conforming UTI for '.pdf', but it's certainly possibly that some app has added the association.
  • On 3, Jan, 2012, at 07:14 PM, Quincey Morris wrote:

    > On Jan 3, 2012, at 02:11 , Martin Hewitson wrote:
    >
    >> Actually, it seems that the NSWorkspace method behaves differently, (assuming I'm using it correctly):
    >>
    >> [[NSWorkspace sharedWorkspace] filenameExtension:@"pdf" isValidForType:(NSString *)kUTTypeText]
    >>
    >> returns YES
    >>
    >> whereas the UTTypeConformsTo() solution returns NO.
    >
    > One thing to consider is that your UTType… approach uses the *preferred* UTI for '.pdf', which may indeed not conform to the text type. But there could be another '.pdf'-based UTI defined on your system that does conform. AFAICT the standard UTIs don't contain a text-conforming UTI for '.pdf', but it's certainly possibly that some app has added the association.
    >
    >

    I see. I don't know how to check that. In any case, I'm left wondering which one to use. I want that .pdf is not a text file, so I guess I'll stick with the UTTypeConformsTo() solution, though this all feels a little fragile. I may make hard-coded exceptions for typical extensions that the app deals with, and always return YES from my -isText method for these particular extensions.

    Thanks for your thoughts,

    Martin

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Martin Hewitson
    Albert-Einstein-Institut
    Max-Planck-Institut fuer
        Gravitationsphysik und Universitaet Hannover
    Callinstr. 38, 30167 Hannover, Germany
    Tel: +49-511-762-17121, Fax: +49-511-762-5861
    E-Mail: <martin.hewitson...>
    WWW: http://www.aei.mpg.de/~hewitson
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • On Jan 3, 2012, at 10:29 , Martin Hewitson wrote:

    > I don't know how to check that. In any case, I'm left wondering which one to use. I want that .pdf is not a text file, so I guess I'll stick with the UTTypeConformsTo() solution, though this all feels a little fragile. I may make hard-coded exceptions for typical extensions that the app deals with, and always return YES from my -isText method for these particular extensions.

    Incidentally, this:

    [[NSWorkspace sharedWorkspace] filenameExtension:@"pdf" isValidForType:(NSString *)kUTTypeText]

    returns NO for me. You could try investigating 'UTTypeCreateAllIdentifiersForTag' to find all of the '.pdf'-associated UTIs on your Mac. That might give a clue. You could also investigate the Finder's "Open With…" menu (for some '.pdf' file), to see what applications have registered themselves as accepting PDF. Perhaps one of them registered a non-standard UTI.
previous month january 2012 next month
MTWTFSS
            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          
Go to today