• 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: UTI Example
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UTI Example


  • Subject: Re: UTI Example
  • From: Jonathan Dann <email@hidden>
  • Date: Sat, 23 Aug 2008 11:52:56 +0100

Reposted due to 25KB size limit. Sorry.

On 23 Aug 2008, at 06:42, Adam Thorsen wrote:
Are there any examples of using the UTI API to determine the UTI given a path to a file?

Thanks,
-Adam

In my app I have a singleton UTI controller class that has an NSArray property of UTIs supported by my application. There's also a dictionary that maps the Core Data entities that I use to represent each file type at runtime. Here are the methods of my UTI controller, hope they help. I've replaced most of the string constants with external declarations already but a couple are still constant strings. They show how to use -[NSWorkspace type:conformsToType:] and -[NSWorkspace typeOfFile:error:]


As for your info.plist file, and exported and imported declarations, there's a good article here:

http://developer.apple.com/documentation/Carbon/Conceptual/understanding_utis/understand_utis_conc/chapter_2_section_4.html

- (id)init;
{
if (![super init])
return nil;
self.supportedUTIs = [NSArray arrayWithObjects:(NSString *)kUTTypePlainText ,kUTTypeTex,kUTTypeTeXShopTex,kUTTypeBib,kUTTypeBibDeskBib,(NSString *)kUTTypePDF,(NSString *)kUTTypeImage,kUTTypeEPS,nil];
self.utiEntityNameMap = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:ESTexFileEntityName ,ESTexFileEntityName ,ESTexFileEntityName ,@"BibTexFile ",@"BibTexFile ",ESPDFFileEntityName,ESImageFileEntityName,@"EPSFile",nil] forKeys:self.supportedUTIs];
return self;
}


- (NSString *)isSupportedUTI:(NSString *)uti error:(NSError **)errorPointer;
{
for (NSString *supportedUTI in self.supportedUTIs)
if ([[NSWorkspace sharedWorkspace] type:uti conformsToType:supportedUTI])
return supportedUTI;
if (errorPointer != NULL)
*errorPointer = [NSError errorWithDomain:ESScribblerErrorDomain code:ESUnsupportedUTIErrorCode userInfo:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString (@"",@""),NSLocalizedDescriptionKey ,NSLocalizedString (@"",@""),NSLocalizedRecoverySuggestionErrorKey ,uti,ESUnsupportedUTIErrorKey,nil]];
return nil;
}


- (NSString *)isSupportedFile:(NSString *)filepath error:(NSError **)errorPointer;
{
NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile:filepath error:errorPointer];
if (!uti)
return nil;
return [self isSupportedUTI:uti error:errorPointer];
}


- (NSString *)entityNameForFile:(NSString *)filepath error:(NSError **)errorPointer;
{
NSString *supportedUTI = [self isSupportedFile:filepath error:errorPointer];
if (!supportedUTI)
return nil;
return [self.utiEntityNameMap valueForKey:supportedUTI]; // leave this error as nil, if we've got this far then we can determine the UTI
}


/*
Maps the UTIs for the files and create a dictionary of entity name values and filepath keys. All errros are put into an NSDictionary. It is guaranteed to return a dictionary, so clients must inspect both the errors dictionary and the count of the returned dictionary.
This method can't reasonably return nil as the presence of errors does not preclude the presence of supported files. To return nil if there are errors is too simplistic.
*/
- (NSDictionary *)entityMapForFiles:(NSArray *)filepaths errors: (NSDictionary **)dictionaryPtr;
{
NSMutableDictionary *mutableErrorsDictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *mutableEntityMap = [NSMutableDictionary dictionary];
for (NSString *filepath in filepaths) {
NSError *entityError = nil;
NSString *entityName = [self entityNameForFile:filepath error:&entityError];
if (!entityName)
[mutableErrorsDictionary setObject:entityError forKey:filepath];
else
[mutableEntityMap setObject:entityName forKey:filepath];
}

if ([mutableErrorsDictionary count] != 0 && dictionaryPtr != NULL)
*dictionaryPtr = [[mutableErrorsDictionary copy] autorelease];
return [[mutableEntityMap copy] autorelease];
}


Jonathan

http://espresso-served-here.com

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: 
 >UTI Example (From: Adam Thorsen <email@hidden>)

  • Prev by Date: Re: NSMenuItem - setHidden
  • Next by Date: Implementing isEqual: and hash
  • Previous by thread: Re: UTI Example
  • Next by thread: textfield not updating with bindings - Whats wrong?
  • Index(es):
    • Date
    • Thread