Hierarchical file system (HFS) type and creator codes are used by applications such as the Finder and Launch Services to identify document files with their associated applications, icons, and so on. By default, applications based on NSDocument do not save HFS type and creator codes in documents. To set the type and creator codes, your NSDocument subclass can override fileAttributesToWriteToURL:ofType:forSaveOperation:originalContentsURL:error: to add type and creator codes for the NSFileHFSTypeCode and NSFileHFSCreatorCode attributes, respectively.
If you want to set the type and creator codes for a file, independent of NSDocument, use the NSFileManager method changeFileAttributes:atPath:.
Listing 1 shows the fileAttributesToWriteToURL:ofType:forSaveOperation:originalContentsURL:error: method of an NSDocument subclass. This implementation assumes that the NSDocument subclass has previously set its type and creator code constants in a manner such as:
const OSType kMyAppCreatorCode = 'Blah'; |
You can modify this fragment to achieve the type and creator code behavior you want in your application.
Listing 1 Saving HFS type and creator information
- (NSDictionary *)fileAttributesToWriteToURL:(NSURL *)absoluteURL |
ofType:(NSString *)typeName |
forSaveOperation:(NSSaveOperationType)saveOperation |
originalContentsURL:(NSURL *)absoluteOriginalContentsURL |
error:(NSError **)outError |
{ |
NSMutableDictionary *fileAttributes = |
[[super fileAttributesToWriteToURL:absoluteURL |
ofType:typeName forSaveOperation:saveOperation |
originalContentsURL:absoluteOriginalContentsURL |
error:outError] mutableCopy]; |
[fileAttributes setObject:[NSNumber numberWithUnsignedInt:kMyAppCreatorCode] |
forKey:NSFileHFSCreatorCode]; |
[fileAttributes setObject:[NSNumber numberWithUnsignedInt:kMyDocumentTypeCode] |
forKey:NSFileHFSTypeCode]; |
return [fileAttributes autorelease]; |
} |
© 2001, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-12)