开发者

How to use NSTask with pbcopy?

开发者 https://www.devze.com 2023-03-06 13:17 出处:网络
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 :

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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消