views:

51

answers:

1

For example, i have this simple bash script:

#!/bin/sh
cd $1;

And this cocoa wrapper for it:

NSTask *cd = [[NSTask alloc] init];

NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test 1"];

[cd setLaunchPath:@"/bin/sh"];

[cd setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle]
                                              pathForResource:@"cd" ofType:@"sh"],testFolder, nil]];
[cd launch];
[cd release];

This is doesn't work correct. And the problem is space in folder name in testFolder. I'm trying to set testFolder like this:

NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test\\ 1"]

But this is also output same error:

cd.sh: line 9: cd: /Users/test/Desktop/test: No such file or directory

Paths without spaces (for example: @"/Users/test/Desktop/test1" ) works as well.

+1  A: 

That's not really a problem about NSTask. Open your terminal (and run bash if you use tcsh), and do

$ mkdir foo\ bar
$ FUBAR=foo\ bar
$ cd $FUBAR

This doesn't work. You need to do

$ cd "$FUBAR"

instead. This is because of the expansion rule of the sh. Read the manual of bash e.g. here, the section called EXPANSION. This section from the shell scripting primer might help, too.

Yuji