开发者

How to make Cocoa document package type with it's extension hidden by default?

开发者 https://www.devze.com 2022-12-19 14:29 出处:网络
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.

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

精彩评论

暂无评论...
验证码 换一张
取 消