Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You can also use pathForResource:eek:fType: to get the file's absolute path directly without string manipulation.
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
Thanks for this:

Code:
NSString *resPath = [[NSBundle mainBundle] resourcePath];

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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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:
NSString *fullResPath = [resPath stringByAppendingPathComponent:@"my.app"];
should work. fullResPath should then contain the complete path of your app for NSTask.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.