• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]


  • Subject: Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]
  • From: Michael Nickerson <email@hidden>
  • Date: Sat, 24 May 2008 02:03:01 -0400


On May 23, 2008, at 5:19 PM, Sean McBride wrote:

On 5/22/08 4:37 PM, Lynn Barton said:

From what I have read quickly since Sean McBride sent his comment, UTIs are
not yet implemented to the point where I could get the UTI of every file on
my computer.

Sure you can. Use LSCopyItemAttributes() with kLSItemContentType.


I've written a category on NSFileManager that, among other things, will do this. Here's the relevant portion for you:


__________________________________________________
#import <Foundation/Foundation.h>

@interface NSFileManager (DSFileAdditions)
+ (NSString *)UTIForFile:(NSString *)path;
+ (NSString *)UTIForURL:(NSURL *)url;
@end

@implementation NSFileManager (DSFileAdditions)

+ (NSString *)UTIForFile:(NSString *)path
{
	return [self UTIForURL:[NSURL fileURLWithPath:path]];
}

+ (NSString *)UTIForURL:(NSURL *)url
{
NSString *retval = nil;
FSRefPtr fileRef = NULL;

fileRef = calloc( 1, sizeof( FSRef ) );

if ( (fileRef != NULL) && (url != nil) && [url isFileURL] && (CFURLGetFSRef( (CFURLRef)url, fileRef ) == true) ) {
CFStringRef theUTI = NULL;
if ( LSCopyItemAttribute( fileRef, kLSRolesAll, kLSItemContentType, (CFTypeRef *)&theUTI ) == noErr ) {
retval = [[(NSString *)theUTI copy] autorelease];
CFRelease( theUTI );
}
}

if ( fileRef != NULL )
free( fileRef );

return retval;
}
__________________________________________________



I *think* this code should work fine under GC (for anyone using it), but you should probably test it first as I haven't.



-------------------------------------- Darkshadow (aka Michael Nickerson) http://www.nightproductions.net


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode] (From: "Sean McBride" <email@hidden>)
 >Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode] (From: Lynn Barton <email@hidden>)
 >Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode] (From: "Sean McBride" <email@hidden>)

  • Prev by Date: Re: NSArray for (... in ...)
  • Next by Date: Re: NSCollectionView problems
  • Previous by thread: Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]
  • Next by thread: Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]
  • Index(es):
    • Date
    • Thread