Re: Can't get file type code using [fileAttr valueForKey:NSFileHFSTypeCode]
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