2011-02-22 91 views
0

我有一个独立的(第三方)应用程序,我尝试使用命令“kick”启动。我已经设置了我的〜/ .bash_profile和/ etc/bashrc文件,这样我就可以在终端窗口中键入kick [command]并且完美地工作。所以我假设我已经正确设置了一切。试图运行NSTask,但得到一个错误

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

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

所以我的命令看起来应该像“踢-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:库未加载:建立/ darwin_x86_64/gcc_opt_dynamic /核心/ libai.dylib

/Users/Gene/Kick/Kick-3.3.4.0/bin/kick

原因:从引用图像没有发现

这kickOut的NSLog的... 踢-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〜/桌面:没有这样的文件或目录

任何帮助表示赞赏。

谢谢百万

回答

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; 

} 
1

执行NSTask时,您的环境设置不会被读取。

几年前我问了this question

+0

感谢您的答复Abizem。我知道〜/ .bash_profile只适用于交互式终端,但是不会/ etc/bashrc适用于所有内容?互动还是非互动? – crewshin 2011-02-22 18:57:17

+0

尝试在〜/ .MacOSX/environment.plist中设置将被读取的路径。 – Abizern 2011-02-22 19:09:50

相关问题