views:

258

answers:

1

I'm making a Cocoa application which uses document package (bundle) as it's data. I specified an extension and Finder now recognizes the folder with the extension as a document well. But the folder's extension is still displaying, and I want to hide it by default (like application bundle) Is there an option to do this?

A: 

You can use the -setAttributes:ofItemAtPath:error: method of NSFileManager to set the file attributes of any file. In this case you want to set the value of the NSFileExtensionHidden key.

To apply this to your saved document, you can override -writeToURL:ofType:error: in your NSDocument subclass and then set the file extension to hidden once the document is saved:

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
    //call super to save the file
    if(![super writeToURL:absoluteURL ofType:typeName error:outError])
        return NO;

    //get the path of the saved file
    NSString* filePath = [absoluteURL path];

    //set the file extension hidden attribute to YES
    NSDictionary* fileAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] 
                                                          forKey:NSFileExtensionHidden];
    if(![[NSFileManager defaultManager] setAttributes:fileAttrs 
                                         ofItemAtPath:filePath
                                                error:outError])
    {
        return NO;
    }
    return YES;
}
Rob Keniger
Thanks! Is this per-file approach or per-type (or extension) approach? Is that impossible setting default behavior of my file type?
Eonil
This is per-file, it sets an extended attribute on the file. I don't think there's a way to set this globally.
Rob Keniger