我是一个初学者,我有一个问题。我想在命令"pbcopy“中使用NSTask。我试过了,但似乎不起作用:
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];发布于 2011-05-16 19:16:46
管道(“\”)是shell的一个特性,而不是您使用的命令的参数。您必须使用两个NSTasks,一个用于回显,另一个用于pbcopy,并在它们之间设置一个NSPipe。
顺便说一句,我假设你只是以这个为例。否则,使用NSPasteboard就更简单了。
https://stackoverflow.com/questions/6022161
复制相似问题