尝试运行 NSTask 但出现错误

发布于 2024-10-18 20:07:12 字数 1884 浏览 0 评论 0原文

我有一个独立(第 3 方)应用程序,我试图使用命令“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: No such file or directory

感谢任何帮助。

感谢一百万

I have a standalone (3rd party) app that I'm trying to launch using a command "kick". I've setup my ~/.bash_profile and /etc/bashrc files so that I can type kick [command] into a terminal window and it works perfectly. So I'm assuming that I have everything setup correctly.

The problem comes when I try and use NSTask.

Essentially what I'm doing is creating two mutable arrays, kickBuild and kickOut. One to assemble the command (it's a string of flags) and one to use with NSTask. I take kickBuild and convert it to a string separated by spaces, then add that as an object into the second array.

So my command should look something like "kick -i /path/to/input/file.ext -as 2 -g 2.2" etc. And if I type this into a terminal window it works great.

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];

When I build my app and run this code, I get this error…

dyld: Library not loaded: build/darwin_x86_64/gcc_opt_dynamic/core/libai.dylib

Referenced from: /Users/Gene/Kick/Kick-3.3.4.0/bin/kick

Reason: image not found

This is an NSLog of kickOut…
kick -i /Users/Gene/Test_Main.0001.ext -r 960 540 -as 2 -g 2.2 -v 5 -dp

Is this something that I'm doing wrong? Or is this an issue with kick?

How would I test NSTask with some basic terminal task to make sure that I'm using NSTask correctly?

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];

If I run this without the @"-c", I get: /bin/sh: ls ~/Desktop: No such file or directory

Any help is appreciated.

Thanks a million

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2024-10-25 20:07:12

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

/*

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;

}

Here's a simple NSTask test that should work for you (note: arguments:(NSArray *)arguments):

/*

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;

}
橘寄 2024-10-25 20:07:12

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

几年前我问过这个问题

Your environment settings are not read when executing NSTask.

I asked this question a couple of years ago.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文