以编程方式生成mac包/ bundle

Rem*_*rrr 10 filesystems macos cocoa objective-c

通过终端你可以使用命令 - "SetFile -a B filename"

以编程方式我认为我应该通过[[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories设置其中一个属性:NO attributes:attributes error:nil];

但我找不到哪一个.

谢谢

Rob*_*ger 12

能够以编程方式设置捆绑位仍然很有用,例如iPhoto执行此操作以使iPhoto Library文件夹显示为单个文件.

您可以使用Carbon File Manager API以编程方式设置捆绑位.您需要确保您的应用程序链接到Carbon框架并导入<Carbon/Carbon.h>标头.这些调用是64位安全的.

- (void)setBundleBitOfFile:(NSString*)path toBool:(BOOL)newValue
{
    const char* pathFSR = [path fileSystemRepresentation];
    FSRef ref;
    OSStatus err        = FSPathMakeRef((const UInt8*)pathFSR, &ref, /*isDirectory*/ NULL);

    if (err == noErr)
    {
        struct FSCatalogInfo catInfo;
        union FinderInfoTransmuter finderInfoPointers = { .bytes = catInfo.finderInfo };

        err = FSGetCatalogInfo(&ref,
                               kFSCatInfoFinderInfo,
                               &catInfo,
                               /*outName*/ NULL,
                               /*FSSpec*/ NULL,
                               /*parentRef*/ NULL);

        if (err == noErr)
        {
            if (newValue)
                finderInfoPointers.finderInfo->finderFlags |=  kHasBundle;
            else
                finderInfoPointers.finderInfo->finderFlags &= ~kHasBundle;

            FSSetCatalogInfo(&ref,
                             kFSCatInfoFinderInfo,
                             &catInfo);
        }
    }
}

- (BOOL)bundleBitOfFile:(NSString*)path
{
    BOOL value          = NO;

    const char* pathFSR = [path fileSystemRepresentation];
    FSRef ref;
    OSStatus err        = FSPathMakeRef((const UInt8*)pathFSR, &ref, /*isDirectory*/ NULL);

    if (err == noErr)
    {
        struct FSCatalogInfo catInfo;
        union FinderInfoTransmuter finderInfoPointers = { .bytes = catInfo.finderInfo };

        err = FSGetCatalogInfo(&ref,
                               kFSCatInfoFinderInfo,
                               &catInfo,
                               /*outName*/ NULL,
                               /*FSSpec*/ NULL,
                               /*parentRef*/ NULL);

        if (err == noErr)
        {
            value = (BOOL)(((finderInfoPointers.finderInfo->finderFlags) & kHasBundle) == kHasBundle);
        }
    }

    return value;
}
Run Code Online (Sandbox Code Playgroud)


bbu*_*bum 5

你想做什么?

几乎所有 Mac OS X 捆绑包实际上都是文件夹,其中包含非常特定的文件/文件夹布局。很少有一个包只是一个文件——它们确实存在,但不经常存在。

在大多数情况下,文件上的 Bundle 位完全无关紧要。

我想将一个文件夹设置为一个包。所以通过文件系统,它看起来像一个文件。后来想用我的应用程序打开那个包。问题是如何通过cocoa设置文件夹bundle属性。

你真的不需要设置捆绑位。只要您的文件夹有一个扩展名(它应该有)并且您的应用程序被正确配置为打开该扩展名的文件/文件夹,它就应该像文件一样出现在查找器中。

例子:

  1. 在 Finder 中转到 ~/Desktop

  2. 在终端中,执行:

  3. cd ~/Desktop

  4. mkdir aafoo

  5. mv aafoo aafoo.rtfd

在 (4) 之后,您将看到“aafoo”作为文件夹显示在 Finder 中。

在 (5) 之后,'aafoo' 将更改为 TextEdit 文档。不需要属性位。


好,可以。你真的很想设置那个位。

鉴于您有命令行来执行此操作,我建议仅使用NSTask。这样做可能比使用 API 更容易。