PDA

View Full Version : Run .app from Resources folder using NSTask




RobRiley
Feb 4, 2009, 09:21 AM
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
Feb 4, 2009, 10:29 AM
Try

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
Feb 4, 2009, 10:36 AM
You can also use pathForResource:ofType: to get the file's absolute path directly without string manipulation.

RobRiley
Feb 4, 2009, 11:16 AM
Thanks for this:


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?

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
Feb 4, 2009, 01:50 PM
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?

NSString *fullResPath = [resPath stringByAppendingPathComponent:@"my.app"];should work. fullResPath should then contain the complete path of your app for NSTask.

RobRiley
Feb 4, 2009, 02:09 PM
That did it! Thanks for your help :)