< Previous PageNext Page > Hide TOC

Saving HFS Type and Creator Codes

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];
}


< Previous PageNext Page > Hide TOC


© 2001, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-12)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.