For a given extension, how do you check whether directories with that extension will be shown by the Finder as a package?
I figured the method below is an effective implementation for this, but creating a temporary directory feels like a hack. I'm guessing I should be able to implement this properly through the Launch Services API, but I can't quite figure out how to do it (I might be overlooking the obvious though).
// Extension method on NSWorkspace
@implementation NSWorkspace (MyExtraMethods)
- (BOOL) isPackageExtension: (NSString*) extension
{
NSString * pathToTemp = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"Untitled" stringByAppendingPathExtension: extension]];
[[NSFileManager defaultManager] createDirectoryAtPath:pathToTemp withIntermediateDirectories:NO attributes:nil error:NULL];
BOOL result = [[NSWorkspace sharedWorkspace] isFilePackageAtPath: pathToTemp];
[[NSFileManager defaultManager] removeItemAtPath:pathToTemp error:NULL];
return result;
}
@end
// Basic test for the above
- (void) testIsPackageExtension
{
STAssertFalse([[NSWorkspace sharedWorkspace] isPackageExtension: @"txt"], @"");
STAssertFalse([[NSWorkspace sharedWorkspace] isPackageExtension: @"rtf"], @"");
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"rtfd"], @"");
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"app"], @"");
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"kext"], @"");
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"clr"], @"");
/* The following tests depend on having applications installed
that are not included in Mac OS X:
.esproj Espresso, tested with version 2.0.5 ( http://macrabbit.com/espresso/ )
.dtps Instruments, included in Xcode, tested with version 4.5 (4523) */
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"esproj"], @"");
STAssertTrue([[NSWorkspace sharedWorkspace] isPackageExtension: @"dtps"], @"");
}
Edit: The test above has been edited to include additional example extensions (original post only used "rtf" and "rtfd").