试图运行NSTask但收到错误

时间:2011-02-22 17:52:07

标签: objective-c nstask

我有一个独立的(第三方)应用程序,我正在尝试使用命令“kick”启动。我已经设置了我的〜/ .bash_profile和/ etc / bashrc文件,这样我就可以在终端窗口中键入kick [command]并且它可以正常工作。所以我假设我已经正确设置了所有内容。

当我尝试使用NSTask时会出现问题。

基本上我正在做的是创建两个可变数组,kickBuild和kickOut。一个用于组装命令(它是一串标志)和一个用于NSTask的命令。我将kickBuild转换为由空格分隔的字符串,然后将其作为对象添加到第二个数组中。

所以我的命令应该看起来像“kick -i /path/to/input/file.ext -as 2 -g 2.2”等。如果我在终端窗口输入它效果很好。

kickBuild = [[NSMutableArray alloc] initWithCapacity:100];
kickOut = [[NSMutableArray alloc] initWithCapacity:2]; // Thinking that this should be "kick" and then "-i /path/to/input/file.ext -as 2 -g 2.2"
kickPath = [kickLocationTextField stringValue]; // This is just the path to my kick executable. NOT /bin/sh. Is that correct?
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

当我构建我的应用程序并运行此代码时,我收到此错误...

dyld:未加载库:build / darwin_x86_64 / gcc_opt_dynamic / core / libai.dylib

参考:/Users/Gene/Kick/Kick-3.3.4.0/bin/kick

原因:未找到图片

这是kickOut的NSLog ... kick -i /Users/Gene/Test_Main.0001.ext -r 960 540 -as 2 -g 2.2 -v 5 -dp

这是我做错了吗?或者这是踢的问题吗?

我如何测试NSTask的一些基本终端任务,以确保我正确使用NSTask?

kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
kickOut = [[NSMutableArray alloc] initWithCapacity:2];
kickPath = @"/bin/sh";
[kickBuild addObject:@"-c"]; // Do I need this?
[kickBuild addObject:@"ls"];
[kickBuild addObject:@"~/Desktop"];
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

如果我在没有@“ - c”的情况下运行,我得到: / bin / sh:ls~ / Desktop:没有这样的文件或目录

感谢任何帮助。

万分感谢

2 个答案:

答案 0 :(得分:1)

执行NSTask时未读取您的环境设置。

几年前我问this question

答案 1 :(得分:1)

这是一个适合您的简单NSTask测试(注意:参数:(NSArray *)参数):

/*

gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o nstask nstask.m

./nstask

http://stackoverflow.com/questions/5081846/trying-to-run-nstask-but-getting-an-error


launchedTaskWithLaunchPath:arguments:

Creates and launches a task with a specified executable and arguments.

+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments


*/

#import <Foundation/Foundation.h>

int main(int argc, const char *argv[])
{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSTask *task = [NSTask new];

NSMutableArray *kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
//NSMutableArray *kickOut = [[NSMutableArray alloc] initWithCapacity:2];
NSString *kickPath = @"/bin/ls";

//[kickBuild addObject:@"/bin/ls"]; // Do I need this?
[kickBuild addObject: [@"~/Desktop" stringByExpandingTildeInPath]];

task = [NSTask launchedTaskWithLaunchPath: kickPath arguments: kickBuild];
[task waitUntilExit];


[pool release];

return 0;

}
相关问题