我在Cocoa項目中使用以下代碼來調用我所做的腳本。該腳本與項目位於同一文件夾中,甚至出現在XCode的「Resources」文件夾下。找到正確的路徑,但它仍然說路徑不可訪問。請幫助。NSTask啓動路徑不可訪問
NSBundle *mainBundle=[NSBundle mainBundle];
NSString *path=[mainBundle pathForResource:@"script" ofType:@"sh"];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: path];
NSLog (path);
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data = [ddFile readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
HDSpeed = [f output];
[f release];
[task release];
我在調試器中得到的輸出是:
2010-07-10 17:53:26.384 tester[5023:a0f] /Users/guest/Library/Developer/Xcode/DerivedData/tester-bhukztmqjwoqrwereagsshvtbfqx/Build/Products/Debug/tester.app/Contents/Resources/script.sh
2010-07-10 17:53:26.386 tester[5023:a0f] launch path not accessible
謝謝!我是OSX的新手,請告訴我bash翻譯的位置在哪裏? – hassaanm 2010-07-11 01:47:17
/bin,除非用戶修改了基本安裝,這在這方面似乎不大可能。 – jer 2010-07-11 01:56:23
哦,謝謝!/bin/bash似乎正常工作,但現在該程序似乎在任務啓動後停止前進。有關這個的任何線索? – hassaanm 2010-07-11 02:02:05