【问题标题】:How to use NSTask with pbcopy?如何将 NSTask 与 pbcopy 一起使用?
【发布时间】: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


    【解决方案1】:

    管道 ("|") 是 shell 的一个特性,而不是您正在使用的命令的参数。您必须使用两个NSTasks,一个用于echo,一个用于pbcopy,并在它们之间设置一个NSPipe

    顺便说一句,我假设您只是以此为例。否则使用NSPasteboard 会更简单。

    【讨论】:

    • pbcopy 位于 /usr/bin,而不是 /bin。
    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2021-01-02
    • 1970-01-01
    • 2023-03-13
    • 2014-09-25
    • 2016-01-30
    相关资源
    最近更新 更多