How to make Cocoa document package type with it's extension hidden by default?
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 sti开发者_Python百科ll displaying, and I want to hide it by default (like application bundle) Is there an option to do this?
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;
}
精彩评论