How to load a text-resource from a bundle (Plugin)

Christiaan Hofman cmhofman at gmail.com
Sat Dec 29 03:32:03 PST 2007


On 29 Dec 2007, at 2:46 AM, Aya Koshigaya wrote:

> Hi,
>
> I have developed a plugin for Autodesk Maya for Windows. I now want  
> to port this plugin to OSX.. and here i have some problems :(
>
> I opened one of the Maya-ExamplePlugins in xCode and then just  
> replaced all the files with my own. So far, no problem.. but I'm  
> using some resources that I included to my plugin.
>
> In Windows I used the WinAPI functions "FindResource" and  
> "LoadResource".. and here's the problem, I have no idea how to port  
> this to osx.
>
> In xCode I added my Resource (It's a textfile "data.txt") and put  
> it in the Resources-Folder so it will be copied to the bundle when  
> building.
>
> I now tried to load the file via Cocoa:
> NSString *nsResource = [[NSBundle mainBundle] 
> pathForResource:@"data" ofType:@"txt"];
>
> But the resource wasn't found, I found out, the problem is  
> "mainBundle" is the Maya-Bundle, not my plugin...
>
>
> After this I tried to find my bundle, but with no result.. :(
> When  I use this:
> [NSBundle bundleForClass:[self class]];
>
> I get a compile-error that says "self is undeclared", because my  
> plugin is still written in C, not ObjC... and I'm using the ObjC  
> functions inside my normal C-functions = no self.
>
> Next try was:
> [NSBundle bundleWithIdentifier:@"de.koshigaya.test"];
>
> the result was NULL.. no bundle with this identifier was found :(
>
> As last thing I tried to list ALL bundles and see if my own one is  
> found with this:
>
> 	NSArray *bundles = [NSBundle allBundles];
> 	for (int i = 0; i < [bundles count]; i++) {
> 		NSBundle *b = [bundles objectAtIndex:i];
> 		NSString *vident = [b bundleIdentifier];
> 		printOut([vident UTF8String]);
> 	}
>
> and... It only listed ONE bundle.. the same as "mainBundle".. ->  
> com.alias.Maya.2008
>
> So... no I have no idea what to do more.. I'm totally helpless..
> Maybe Maya isn't realy "loading" my Bundle..? I mean, maybe Maya  
> uses a way to load the bundle so it's not registered or something..  
> don't know.. :/
>
> Does anyone know what I can do? Help...
>
> Kind regards,
>  Aya

+[NSBundle mainBundle] gives you the bundle of the app in which your  
plugin is loaded, so that indeed won't work. You should use either + 
[NSBundle bundleWithIdentifier:] or +[NSBundle bundleForClass:] to  
get the bundle of your plugin.

Christiaan



More information about the MacOSX-dev mailing list