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

  • 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
  • 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
  • Hi,

    thanks for your answers :)

    > You might want to retry using the
    > [NSBundle bundleForClass:NSClassFromString(@"YourClassName")]

    And what should I use as ClassName? I don't have any ObjC Classes..
    only normal C-Classes.. can I use these too?

    > +[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.

    I tried these two allready,both weren't working for me (see the
    original mail, there I said why I had problems with these two)

    Regards,
    Aya

    On Dec 29, 2007, at 11:46, scott little wrote:

    > Aya,
    >
    > You might want to retry using the
    >
    > [NSBundle bundleForClass:NSClassFromString(@"YourClassName")]
    >
    > This might work for you,
    >
    > scott
    > --
    > “To announce there must be no criticism of the President, and to
    > stand by the President, right or wrong, is not only unpatriotic and
    > servile, it is morally treasonous to the American public.”
    > President Theodore Roosevelt
    > --
    > scott little
    > <slittle...>
    > --
    > sadly no music right now: iTunes is Stopped
    >
    >
    >
    > On 29/12/2007, at 02:46, 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
    >> _______________________________________________
    >> MacOSX-dev mailing list
    >> <MacOSX-dev...>
    >> http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >>
    >
  • So does your bundle have the proper layout for a macosx bundle,
    including the identifier in the info.plist?

    Christiaan

    On 29 Dec 2007, at 4:28 PM, Aya Koshigaya wrote:

    > Hi,
    >
    > thanks for your answers :)
    >
    >> You might want to retry using the
    >> [NSBundle bundleForClass:NSClassFromString(@"YourClassName")]
    >
    >
    > And what should I use as ClassName? I don't have any ObjC Classes..
    > only normal C-Classes.. can I use these too?
    >
    >
    >> +[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.
    >
    >
    > I tried these two allready,both weren't working for me (see the
    > original mail, there I said why I had problems with these two)
    >
    > Regards,
    > Aya
    >
    >
    > On Dec 29, 2007, at 11:46, scott little wrote:
    >
    >> Aya,
    >>
    >> You might want to retry using the
    >>
    >> [NSBundle bundleForClass:NSClassFromString(@"YourClassName")]
    >>
    >> This might work for you,
    >>
    >> scott
    >> --
    >> “To announce there must be no criticism of the President, and to
    >> stand by the President, right or wrong, is not only unpatriotic
    >> and servile, it is morally treasonous to the American public.”
    >> President Theodore Roosevelt
    >> --
    >> scott little
    >> <slittle...>
    >> --
    >> sadly no music right now: iTunes is Stopped
    >>
    >>
    >>
    >> On 29/12/2007, at 02:46, 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
    >>> _______________________________________________
    >>> MacOSX-dev mailing list
    >>> <MacOSX-dev...>
    >>> http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >>>
    >>
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • Il giorno 29/dic/07, alle ore 16:28, Aya Koshigaya ha scritto:

    > And what should I use as ClassName? I don't have any ObjC Classes..
    > only normal C-Classes.. can I use these too?

    This confuses me greatly, as C does not have classes. Maybe you're
    referring to C++?

    Anyway, if your bundle has an identifier set correctly in Info.plist,
    you can use [NSBundle bundleWithIdentifier:@"identifier"] to retrieve
    a NSBundle object for it. You should see <http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConfig/
    BPRuntimeConfig.html
    >, which explains most of it (the relevant key is CFBundleIdentifier).

      - ∞
  • On 29.12.2007, at 2:46 , Aya Koshigaya wrote:
    > But the resource wasn't found, I found out, the problem is
    > "mainBundle" is the Maya-Bundle, not my plugin...

      That's to be expected.

    > 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.

      So, declare a class. Just any dummy class will do. Then you can pass
    [AYKDummyClass class] to +bundleWithClass: and get a bundle.

    > Next try was:
    > [NSBundle bundleWithIdentifier:@"de.koshigaya.test"];
    >
    > the result was NULL.. no bundle with this identifier was found :(

      Double-click the "Target" for the plugin you're compiling. Go to the
    "Properties" tab and make sure you've specified de.koshigaya.test as
    the bundle ID. Is it in there?

    > 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.. :/

      Well, IIRC Maya is a Unix app. It could be that it is really using
    dyld directly, or maybe libdl or something like that. If that's the
    case, then there may not be a bundle for your plugin. Have you looked
    at the CFBundle functions, dyld and libdl docs to see if any of these
    provides a way to e.g. get the file for a loaded function?

    Cheers,
    -- M. Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today