我是初学者,我有一个问题。我想将 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];