如何将 NSTask 与 pbcopy 一起使用?
我是初学者,我有一个问题。我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
管道符(“|”)是 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 anNSPipe
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.