views:

19

answers:

1

In a Cocoa app generally we can install a plugin bundle in one of a number of places. If for example the app is called "MyApp" you'd be able to install the plugin at:

  • /Applications/MyApp.app/Contents/PlugIns
  • ~/Library/Application Support/MyApp/PlugIns
  • /Library/Application Support/MyApp/PlugIns
  • /Network/Library/Application Support/MyApp/PlugIns

I'm building an NSArray of paths to search in the correct order but I'm pretty sure I'm doing this wrong since it feels like I'm doing too much work for something Apple seem to provide a lot of functions for.

NSArray *systemSearchPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSAllDomainsMask, YES);
NSMutableArray *searchPaths = [NSMutableArray array];

NSFileManager *fileManager = [NSFileManager defaultManager];

for (NSString *systemPath in systemSearchPaths) {
    NSString *systemPluginsPath = [systemPath stringByAppendingPathComponent:@"PlugIns"];
    // FIXME: Remove debug code
    NSLog(@"Considering plugin path %@", systemPluginsPath);
    if ([fileManager fileExistsAtPath:systemPluginsPath]) {
        [searchPaths addObject:systemPluginsPath];
    }
}

[searchPaths addObject:[[NSBundle mainBundle] builtInPlugInsPath]];

This results in the Array returned by NSSearchPathForDirectoriesInDomains, with the builtInPlugInsPath value appended to the end.

However, it actually searches directories like "~/Library/Application Support/PlugIns" (missing the "MyApp") folder. Before I start hacking the code to inject the name of my application (which is subject to change at any time), am I doing this wrong?

Is there a way to just tell Cocoa "give me all search paths for 'PlugIns'" directories for this application"?

+1  A: 

Nope. You're doing it right.

You can get the name of your application at run time by asking the main bundle for its info dictionary and looking for kCFBundleNameKey therein. When you rename your application, change the bundle name in your Info.plist.

(Definitely do not use your application's filename, as that's much more fragile.)

Be aware that users might not like it if the plug-ins they installed stop working because you renamed your application.

Note that your code above will not catch the PlugIns folder inside the application bundle. For that, ask your main bundle for its built-in plug-ins path or URL.

Peter Hosey
Thank you. I got this working nicely, first checking all the system paths as indicated above, then checking the buildInPlugInsPath as returned by the main bundle. Thanks for the advice on the naming. I'll change my code, since currently is uses the CFBundleExecutable value, not the bundle name itself.
d11wtq