如何将 NSTask 与 pbcopy 一起使用?

发布于 2024-11-07 18:15:53 字数 824 浏览 0 评论 0原文

我是初学者,我有一个问题。我想将 NSTask 与命令“pbcopy”一起使用。我尝试过,但似乎不起作用:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/echo"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"my-text-to-copy", @"| pbcopy", nil];
[task setArguments: arguments];

[task launch];

有什么想法吗?谢谢。


它工作正常:

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

NSPipe *pipe;
pipe = [NSPipe pipe];

task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/echo"];
[task setStandardOutput:pipe]; // write to pipe
[task setArguments: [NSArray arrayWithObjects: @"tmp", nil]];
[task launch];
[task waitUntilExit];

task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/pbcopy"];
[task setStandardInput:pipe]; // read from pipe
[task launch];
[task waitUntilExit];

I am a beginner and I have a problem. I would like to use NSTask with the command "pbcopy". I tried this but it seems that it doesn't work :

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/echo"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"my-text-to-copy", @"| pbcopy", nil];
[task setArguments: arguments];

[task launch];

Any ideas ? Thanks.


It works fine :

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

NSPipe *pipe;
pipe = [NSPipe pipe];

task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/echo"];
[task setStandardOutput:pipe]; // write to pipe
[task setArguments: [NSArray arrayWithObjects: @"tmp", nil]];
[task launch];
[task waitUntilExit];

task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/pbcopy"];
[task setStandardInput:pipe]; // read from pipe
[task launch];
[task waitUntilExit];

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

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

发布评论

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

评论(1

大海や 2024-11-14 18:15:53

管道符(“|”)是 shell 的一项功能,而不是您正在使用的命令的参数。您必须使用两个 NSTasks,一个用于 echo,一个用于 pbcopy,并在它们之间设置一个 NSPipe。

顺便说一句,我假设您只是以此为例。否则,使用 NSPasteboard 会更简单。

The pipe ("|") is a feature of the shell, not an argument to the command you're using. You have to use two NSTasks, one for echo and one for pbcopy and set up an NSPipe between them.

Btw, I'm assuming that you're just using this as an example. Otherwise it would be much simpler to use NSPasteboard for this.

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