discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Running a command using NSTask


From: Richard Frith-Macdonald
Subject: Re: Running a command using NSTask
Date: Sun, 21 Nov 2010 10:15:19 +0000

On 21 Nov 2010, at 08:34, Csanyi Pal wrote:

> Hi,
> 
> I'm trying to run a command from my app using NSTask.
> Here is the part od the code:
> 
> [code]
> - (void) DPbitSetReset: (id)sender
> {
>  NSSound *DPbitReset = [NSSound soundNamed: @"DPbitReset.wav"];
>  NSSound *DPbitSet = [NSSound soundNamed: @"DPbitSet.wav"];
> 
>  NSTask *DPSetBit0 = [[NSTask alloc] init];
>  [DPSetBit0 setLaunchPath: @"/usr/local/bin/DPSetBit0"];

Well, the hard-coded launch path is not portable, and would definitely be wrong 
on ms-windows for instance, so that might be an issue.

A portable way to determine the correct path to an executable might be to use 
NSSearchPathForDirectoriesInDomains() to get a list of the directories the 
program might be in, then use [NSString-stringByAppendingPathComponent:] to 
append your program name to each directory in turn, and use 
[NSFileManager-isExecutableFileAtPath:] to check each to see if the program is 
there.

Alternatively, you could use the GNUstep extension [NSTask+launchPathForTool:] 
(declared in GNUstepBase/NSTask+GNUstepBase.h) method to do all that for you.

Assuming that you have the correct path to the executable, then I guess the 
thing to do is check the return status of the subtask ...
Call [NSTask-waitUntilExit] to wait for the task to exit, then call 
[NSTask-terminationStatus] and perhaps [NSTask-terminationReason] to see 
how/why the subtask terminated.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]