在最近的苹果文档都NSTask和Process有一些过时的方法和属性,虽然没有什么标有API Availability Macro.
实例属性
@property(copy) NSString *launchPath;
@property(copy) NSString *currentDirectoryPath;
var launchPath: String? { get set }
var currentDirectoryPath: String { get set }
Run Code Online (Sandbox Code Playgroud)
实例方法
- (void)launch;
func launch()
Run Code Online (Sandbox Code Playgroud)
类型方法
+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path
arguments:(NSArray<NSString *> *)arguments;
class func launchedProcess(launchPath path: String,
arguments: [String]) -> Process
Run Code Online (Sandbox Code Playgroud)
似乎没有替代品可用,所以给出了什么?
我试图弄清楚如何在提示时将输入传递给NSTask.
例:
我做的事情
kinit username@DOMAIN
Run Code Online (Sandbox Code Playgroud)
然后我收到"输入密码"提示.我希望能够为该NSTask提供密码.
有谁知道如何做到这一点?(基本上通过可可应用程序自动化过程).
谢谢!
我正在尝试使用NSTask运行一个简单的bash脚本并将输出定向到文本视图.执行任务后,我的应用程序的CPU使用率为100%,即使它很简单echo(暂时).
我创建了一个全新的项目来隔离问题:
@interface AppDelegate ()
@property (nonatomic) NSTask *task;
@property (nonatomic) NSPipe *pipe;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.pipe = [NSPipe pipe];
self.pipe.fileHandleForReading.readabilityHandler = ^(NSFileHandle *h) {
NSLog(@"Read: %@", [h readDataToEndOfFile]);
};
self.task = [[NSTask alloc] init];
self.task.launchPath = @"/bin/bash";
self.task.arguments = @[@"-c", @"echo test"];
self.task.standardOutput = self.pipe;
[self.task launch];
}
@end
Run Code Online (Sandbox Code Playgroud)
它被正确执行并输出(作为NSData)记录NSLog:
PipeTest[3933:2623] Read: <74657374 0a>
Run Code Online (Sandbox Code Playgroud)
但是,在我终止我的应用程序之前,CPU使用率保持在100%.
编辑:
Time Profiler测试返回下面的列表,但我不知道如何解释这个.

我一直试图用最近的NSUserScriptTask类及其子类来做(看到这个和这个),到目前为止我已经解决了一些问题,但还有一些问题仍有待解决.从文档中可以看出,NSUserScriptTask不允许取消任务.因此,我决定创建一个简单的可执行文件,将脚本的路径作为参数并运行脚本.这样,我可以使用NSTask从我的主应用程序启动帮助程序,并[task terminate]在必要时调用.但是,我要求:
主应用程序的代码很简单:只需使用正确的信息启动NSTask即可.这就是我现在所拥有的(为了简单起见,我忽略了安全范围的书签之类的代码,这些代码都没有问题.但是不要忘记这是运行沙盒的):
// Create task
task = [NSTask new];
[task setLaunchPath: [[NSBundle mainBundle] pathForResource: @"ScriptHelper" ofType: @""]];
[task setArguments: [NSArray arrayWithObjects: scriptPath, nil]];
// Create error pipe
NSPipe* errorPipe = [NSPipe new];
[task setStandardError: errorPipe];
// Create output pipe
NSPipe* outputPipe = [NSPipe new];
[task setStandardOutput: outputPipe];
// Set termination handler
[task setTerminationHandler: ^(NSTask* task){
// Save output
NSFileHandle* outFile = [outputPipe fileHandleForReading];
NSString* output = [[NSString alloc] initWithData: …Run Code Online (Sandbox Code Playgroud) 好.有关此问题的堆栈溢出有几个问题.这个问题是最接近地雷的唯一问题,但它使用通知.
代码很简单.创建一个新的空Mac OSX项目,只需在applicationDidFinishLaunching:方法中粘贴以下代码即可.它应该获取任何可执行文件的路径(在本例中为GIT).
NSTask *aTask = [[NSTask alloc] init];
NSPipe *outputPipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
[aTask setStandardOutput: outputPipe];
[aTask setStandardError: errorPipe];
[aTask setArguments:[NSArray arrayWithObject:@"which"]];
[aTask setLaunchPath:@"/usr/bin/git"];
NSFileHandle *outputFileHandler = [outputPipe fileHandleForReading];
NSFileHandle *errorFileHandler = [errorPipe fileHandleForReading];
[aTask launch];
[aTask waitUntilExit];
// Task launched now just read and print the data
NSData *data = [outputFileHandler readDataToEndOfFile];
NSString *outPutValue = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSData *errorData = [errorFileHandler readDataToEndOfFile];
NSString …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我正在开发的Mac应用程序中运行终端命令.终端命令是:
sudo sysctl -w net.inet.tcp.delayed_ack=0
Run Code Online (Sandbox Code Playgroud)
我尝试过使用NSTask,但似乎每次都在做错事.
我只想运行此命令并打印输出.感谢您的关注.
PS.这是我当前的代码看起来像(感谢您的回复):
let task = NSTask()
task.launchPath = "/usr/sbin/sysctl"
task.arguments = ["-w", "net.inet.tcp.delayed_ack=0"]
let pipe = NSPipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
Run Code Online (Sandbox Code Playgroud)
我在输出中收到以下消息:
net.inet.tcp.delayed_ack:3
sysctl:net.inet.tcp.delayed_ack = 0:不允许操作
我所拥有的是NSTask运行一个长预制的shell脚本,我希望NSProgressIndicator检查完成了多少.我尝试过很多东西,但似乎无法让它发挥作用.如果进度条不确定但我希望它随着任务的进行而加载,我知道如何使用它.
以下是我运行脚本的方法:
- (IBAction)pressButton:(id)sender {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"script" ofType:@"sh"], nil]];
[task launch];
}
Run Code Online (Sandbox Code Playgroud)
我需要设置一个进度条来检查该任务发生的进度并相应地更新.
我想在我的Xcode/Cocoa项目中嵌入一个命令行可执行文件,然后用NSTask启动它.我应该在setLaunchPath中使用哪条路径?
谢谢 !
我正在编写一个Cocoa应用程序,它需要执行一个UNIX程序并逐行读取它的输出.我设置了NSTask和NSPipe:
task = [[NSTask alloc] init];
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
//... later ...
[task setArguments:...];
[task setLaunchPath:@"..."];
[task launch];
handle = [[task fileHandleForReading] retain];
Run Code Online (Sandbox Code Playgroud)
该命令不会终止,直到程序通知它为止[task terminate].我试图从手柄读取几种方法,如-readInBackgroundAndNotify,while([(data = [handle availableData]) length] > 0)和-waitForDataInBackgroundAndNotify,但管道似乎从来没有得到任何数据.有什么方法可以"戳" NSTask或NSPipe通过刷新数据?
编辑:用-readInBackgroundAndNotify:
[handle readInBackgroundAndNotify];
notification_block_t handlerBlock =
^(NSNotification *notification) {
NSData *data = [[notification userInfo]
objectForKey: NSFileHandleNotificationDataItem];
/*... do stuff ...*/
[self addNotification: handle block: handlerBlock];
};
[self addNotification: handler block: handlerBlock]; …Run Code Online (Sandbox Code Playgroud) 我正在使用NSTask来运行rsync,我希望状态显示在窗口内滚动视图的文本视图中.现在我有这个:
let pipe = NSPipe()
task2.standardOutput = pipe
task2.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSASCIIStringEncoding)! as String
textView.string = output
Run Code Online (Sandbox Code Playgroud)
这就是关于转移的一些统计数据,但是我想实时获得输出,比如当我在Xcode中运行应用程序时打印出来的内容,并将其放入文本视图中.有没有办法做到这一点?
nstask ×10
cocoa ×7
macos ×5
objective-c ×5
swift ×3
nspipe ×2
xcode ×2
executable ×1
nstextview ×1
terminal ×1