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

孤街醉人 提交于 2019-12-24 08:58:01

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/2192295/how-to-make-cocoa-document-package-type-with-its-extension-hidden-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!