Run .app from Resources folder using NSTask

Discussion in 'Mac Programming' started by RobRiley, Feb 4, 2009.

  1. macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #1
    Hi,

    I'm a noobie so be nice :)

    I'm creating a basic Cocoa app that needs to run another app from a menu command. I want to include that app inside the bundle's resources folder but I only know how to execute this using a full path. Here is my code thus far:

    @implementation AppController
    - (IBAction)RunApp:(id)sender{
    NSTask *run;
    run=[[NSTask alloc] init];
    [run setLaunchPath: @"/usr/bin/open"];
    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"/PATH/TO/APP.app", nil];
    [run setArguments: arguments];
    [run launch];
    [run release];
    }
    @end

    How can I replace the full PATH/TO/APP with a relative path to the Contents/Resources folder of my app? Also, if my code is overly complicated, unnecessary or stupid in any way, feel free to suggest improvements.

    Many thanks.
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    Try

    Code:
    NSString *resPath = [[NSBundle mainBundle] resourcePath];
    And then add on your app name using stringWithFormat: or something. There might be a string constant for the Resources path too that gets set when your app launches, I'm not sure about that.
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    You can also use pathForResource:eek:fType: to get the file's absolute path directly without string manipulation.
     
  4. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #4
    Thanks for this:

    How would I then include resPath in the arguments variable? For example I've tried the following which of course doesn't work. What is the correct Syntax?

    Code:
    arguments = [NSArray arrayWithObjects: @*resPath,@"my.app", nil];
    Basically I want to create the string /path/to/resources/my.app to pass as an argument to the unix open command.

    Thanks again.
     
  5. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    Code:
    NSString *fullResPath = [resPath stringByAppendingPathComponent:@"my.app"];
    should work. fullResPath should then contain the complete path of your app for NSTask.
     
  6. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #6
    That did it! Thanks for your help :)
     

Share This Page