Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

objective c - make mac package/bundle programmatically

through terminal you can make it with a command - "SetFile -a B filename"

programmatically i think i should set one of the attributes through [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:attributes error:nil];

but i can't find which one.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is still useful to be able to set the bundle bit programmatically, for instance iPhoto does this to make the iPhoto Library folder appear as a single file.

You can set the bundle bit programmatically using the Carbon File Manager API. You'll need to make sure your app links against the Carbon framework and import the <Carbon/Carbon.h> header. These calls are 64-bit safe.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...