tags:

views:

46

answers:

2

Here is my code:

-(void)startTask{
NSString * cmd = @"/bin/sh";
pty_ = [[PseudoTTY alloc] init];

NSTask * task = [[NSTask alloc] init];
[task setStandardInput:[pty_ slaveFileHandle]];
[task setStandardOutput:[pty_ slaveFileHandle]];
[task setStandardError:[pty_ slaveFileHandle]];

[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];
[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(didRead:)
                   name:NSFileHandleReadCompletionNotification
                 object:[pty_ masterFileHandle]];

[[pty_ masterFileHandle] readInBackgroundAndNotify];

[task launch];

[self insertText:
    [NSString stringWithFormat:@"Started %@ on terminal %@", cmd, [pty_ name]]];

}

But, instead of this, I need it to start an SH file: /applications/brain/server.sh

I'm confused....

Can someone help me with my code?

thanks, Elijah

A: 
[task setCurrentDirectoryPath:[@"~" stringByExpandingTildeInPath]];

You can just call NSHomeDirectory to get the home directory path.

[task setLaunchPath:@"/bin/sh /applications/jarvis/brain/server.sh"];

This file does not exist. There is no directory named “sh ” within the /bin directory; as such, there is no “applications” subdirectory within that, no “jarvis” subdirectory within that, no “brain” subdirectory within that, and no “server.sh” file within that.

Remember that NSTask is not the shell. Shell tricks don't work on it; it doesn't parse arguments, interpolate environment variables or ~ (notice that you had to do that explicitly), or anything else the shell does. You can only use the shell the same as any other program.

You need to set the launch path to the path to the shell, and pass the path to the shell script as the first argument. Alternatively, if you know that the shell script file is executable (and has the correct shebang in it), you can pass the path to the script file as the launch path and omit the arguments.

Peter Hosey
A: 

Try:

// make sure server.sh begins with #!/bin/sh
NSString * cmd = @"/applications/brain/server.sh";  
// ...
[task setLaunchPath:cmd];

(original code from: http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip)

marcus