如何将文件夹标记为包?

Pie*_*ard 6 cocoa

我曾经认为文件夹需要有一个扩展名,以便Finder将它们识别为包.该扩展将在拥有应用程序的Info.plist中声明.

显然还有另一种更优雅的方式,但我无法弄清楚它是如何完成的.

例如,Finder将iPhoto图库视为包.但它没有延伸.mdls显示它确实在内容类型树中有"com.apple.package".实际内容类型是动态分配的.

iPhoto如何创建这样的目录?

Mik*_*lah 7

虽然你不应该完全依赖它,但要做的一件事是设置文件的bundle位.我在NSWorkspace上有一个类别可以做到这一点:

- (void)setBundleBit:(BOOL)flag forFile:(NSString *)path
{
    FSRef fileRef;
    OSErr error = FSPathMakeRef((UInt8 *)[path fileSystemRepresentation], &fileRef, NULL);

    // Get the file's current info
    FSCatalogInfo fileInfo;
    if (!error)
    {
        error = FSGetCatalogInfo(&fileRef, kFSCatInfoFinderInfo, &fileInfo, NULL, NULL, NULL);
    }

    if (!error)
    {
        // Adjust the bundle bit
        FolderInfo *finderInfo = (FolderInfo *)fileInfo.finderInfo;
        if (flag) {
            finderInfo->finderFlags |= kHasBundle;
        }
        else {
            finderInfo->finderFlags &= ~kHasBundle;
        }

        // Set the altered flags of the file
        error = FSSetCatalogInfo(&fileRef, kFSCatInfoFinderInfo, &fileInfo);
    }

    if (error) {
        NSLog(@"OSError %i in -[NSWorkspace setBundleBit:forFile:]", error);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 历史记录:bundle bit最初用于文件,意味着文件中有一个'BNDL'资源.Finder将从遇到的任何文件中读取bundle资源,该文件已设置bundle位且没有设置inited位.现在,'BNDL'资源的功能由Info.plist文件完成,bundle bit主要用于目录,它告诉Finder和Navigation Services将目录视为文件(即,它使得目录一个包). (2认同)