How do I flag a folder as being a package?
Asked Answered
W

4

6

I used to think that folders needed to have an extension so that they are recognized as packages by the Finder. That extension would be declared in the owning application's Info.plist.

Obviously there is another, more elegant way, but I can't figure out how it is done.

E.g. the iPhoto Library is being treated as a package by the Finder. Yet it has no extension. mdls reveals that it indeed has "com.apple.package" in the content type tree. The actual content type is dynamically assigned.

How did iPhoto go about to create such a directory?

Waldowaldon answered 18/11, 2009 at 14:29 Comment(0)
E
7

Although you should not rely solely upon it, one thing to do is set the file's bundle bit. I've got a category on NSWorkspace to do just that:

- (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);
    }
}
Emulsifier answered 18/11, 2009 at 15:16 Comment(3)
Historical note: The bundle bit was originally used for files, and meant that the file had a 'BNDL' resource in it. The Finder would read the bundle resource from any file it encountered that had the bundle bit set and didn't have the inited bit set. Now, the function of 'BNDL' resources is fulfilled by Info.plist files, and the bundle bit is mainly used on directories, where it tells the Finder and Navigation Services to treat the directory as they would a file (i.e., it makes the directory a package).Lumber
Any particular reason why do you put this on NSWorkspace? it doesn't use NSWorkspace's "self" pointer.Ephram
I figure it should be either NSWorkspace or NSFileManager to match the rest of Cocoa. Can't recall why I settled on the former though. Admittedly since this code was written, it might be better on NSURL.Emulsifier
L
2

File and protocol are, to the best of my knowledge, only setup inside your apps Info.plist.

Apps get scanned, and the info in their plists get's added to LaunchServices settings.

Linkinus has 6 different Document Types declared, TextMate has 97, and CyberDuck has 3. I'll do some more searching but I think this is likely the preferred method.

Luxor answered 18/11, 2009 at 15:2 Comment(0)
T
2

You can set the folder’s “package bit” (as mentioned in Bundle Programming Guide):

NSURL *somePath = ...;
NSError *error = nil;
[somePath setResourceValues:@{NSURLIsPackageKey : @YES} error:&error];
if (error != nil) {
    // handle error
}
Trebizond answered 14/10, 2024 at 14:28 Comment(1)
Thanks - this is indeed the correct modern way to do it.Lissie
T
1

There are two commands that may be of interest:

GetFileInfo iPhoto\ Library

attributes: avBstclinmedz

The B says that the "bundle" bit is set. The SetFile command lets you set them. These let you access the extended attributes in HFS+ (per the man page).

Here are the possible attributes:

A   Alias file
B   Bundle
C   Custom icon*
D   Desktop*
E   Hidden extension*
I   Inited*
M   Shared (can run multiple times)
N   No INIT resources
L   Locked
S   System (name locked)
T   Stationery
V   Invisible*
Z   Busy*
Terrify answered 22/5, 2010 at 6:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.