【发布时间】:2011-05-16 19:10:45
【问题描述】:
我是初学者,我有一个问题。我想将 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];
【问题讨论】:
标签: objective-c macos shell foundation pbcopy