【问题标题】:How to make Cocoa document package type with it's extension hidden by default?如何使 Cocoa 文档包类型默认隐藏其扩展名?
【发布时间】:2010-02-09 09:00:08
【问题描述】:

我正在制作一个使用文档包(包)作为数据的 Cocoa 应用程序。 我指定了一个扩展名,Finder 现在可以很好地将具有扩展名的文件夹识别为文档。 但是文件夹的扩展仍然显示,我想默认隐藏它(如应用程序包)有没有一个选项可以做到这一点?

【问题讨论】:

    标签: cocoa document bundle


    【解决方案1】:

    您可以使用NSFileManager-setAttributes:ofItemAtPath:error: 方法来设置任何文件的文件属性。在这种情况下,您需要设置 NSFileExtensionHidden 键的值。

    要将其应用于您保存的文档,您可以在您的 NSDocument 子类中覆盖 -writeToURL:ofType:error:,然后在保存文档后将文件扩展名设置为隐藏:

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

    【讨论】:

    • 谢谢!这是按文件方法还是按类型(或扩展名)方法?这不可能设置我的文件类型的默认行为吗?
    • 这是每个文件的,它设置文件的扩展属性。我认为没有办法在全局范围内设置它。
    猜你喜欢
    • 2020-10-16
    • 2020-04-22
    • 1970-01-01
    • 2021-04-27
    • 2010-09-18
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多