nstask

NSTask blocking the main thread

天大地大妈咪最大 提交于 2019-12-03 14:49:40
I'm using NSTask, but when I launch the task it blocks the main thread (so I can't update it) until the task ends. This is my code: NSString *hostsforping = @"google.es"; pingdata = [[NSTask alloc] init]; [pingdata setLaunchPath: @"/sbin/ping"]; NSArray *pingargs; pingargs = [NSArray arrayWithObjects: @"-c 5", hostsforping, nil]; [pingdata setArguments: pingargs]; NSPipe *pingpipe; pingpipe = [NSPipe pipe]; [pingdata setStandardOutput: pingpipe]; NSFileHandle *pingfile; pingfile = [pingpipe fileHandleForReading]; [pingdata launch]; NSData *pingdata1; pingdata1 = [pingfile readDataToEndOfFile];

How to embed an executable in my project

偶尔善良 提交于 2019-12-03 14:23:46
I would like to embed a command-line executable in my Xcode/Cocoa project, to then start it with NSTask. Which path should I use in the setLaunchPath ? Thanks ! Ernesto you should add it to your resources folder. Then, in runtime, read the app's resource bundle path, and append the name of the executable (including subfolders if you add it to a folder inside the resource bundle) For instance: NSString *execPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"binaryname"]; NSTask *task = [[NSTask alloc] init]; [task setLaunchPath: execPath]; 来源: https://stackoverflow

Run a terminal command in a Mac app with Swift Xcode

孤者浪人 提交于 2019-12-03 13:52:56
I am trying to run a terminal command in a Mac app that I am developing. The terminal command is: sudo sysctl -w net.inet.tcp.delayed_ack=0 I have tried using NSTask but it seems that I am doing something wrong every time. I just want to run this command and print the output. Thank you for your attention. PS. here is what my current code looks like (thanks to your replies): 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

Async execution of shell command not working properly

心不动则不痛 提交于 2019-12-02 12:34:26
问题 So, this is my code : - (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args { NSLog(@"\nRunning ::\n\tCmd : %@\n\tArgs : %@",cmd, args); [theSpinner start]; if (task) { [task interrupt]; } else { task = [[NSTask alloc] init]; [task setLaunchPath:cmd]; [task setArguments:args]; [pipe release]; pipe = [[NSPipe alloc] init]; [task setStandardOutput:pipe]; NSFileHandle* fh = [pipe fileHandleForReading]; NSNotificationCenter* nc; nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self

Objective-C: NSCommand “airport -s” returning empty

本小妞迷上赌 提交于 2019-12-02 10:57:58
问题 I'm trying to run airport command to scan my wireless networks. Right now, the approach is to use NSTask. I'm running it as follow: NSString *command = [NSString stringWithFormat:@"/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s"]; NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:@"/bin/sh"]; NSArray *args = [NSArray arrayWithObjects:@"-c", command, nil]; [task setArguments: args]; NSPipe *pipe = [NSPipe pipe]; [task setStandardOutput:

Objective-C: NSCommand “airport -s” returning empty

做~自己de王妃 提交于 2019-12-02 05:19:42
I'm trying to run airport command to scan my wireless networks. Right now, the approach is to use NSTask. I'm running it as follow: NSString *command = [NSString stringWithFormat:@"/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s"]; NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:@"/bin/sh"]; NSArray *args = [NSArray arrayWithObjects:@"-c", command, nil]; [task setArguments: args]; NSPipe *pipe = [NSPipe pipe]; [task setStandardOutput: pipe]; [task launch]; [task waitUntilExit]; NSData *data = [[pipe fileHandleForReading]

Async execution of shell command not working properly

走远了吗. 提交于 2019-12-02 04:16:35
So, this is my code : - (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args { NSLog(@"\nRunning ::\n\tCmd : %@\n\tArgs : %@",cmd, args); [theSpinner start]; if (task) { [task interrupt]; } else { task = [[NSTask alloc] init]; [task setLaunchPath:cmd]; [task setArguments:args]; [pipe release]; pipe = [[NSPipe alloc] init]; [task setStandardOutput:pipe]; NSFileHandle* fh = [pipe fileHandleForReading]; NSNotificationCenter* nc; nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; [nc addObserver:self selector:@selector(dataReady:) name:NSFileHandleReadCompletionNotification

Swift 2.1 OSx shell commands using NSTask work when run from xcode, but not when exported

老子叫甜甜 提交于 2019-12-02 04:15:55
问题 I wrote a simple OSx (10.11) application to execute shell commands when a button is pressed. It works when I run it from xcode, but when I export the application via "archive", one of the buttons no longer works. I don't get an error and I don't get any output either. I am using absolute paths so I don't understand why it works in xcode but not as an exported application, Nor do I understand why one button works and the other doesn't. Here is the main function that I am using the make the

PHP CLI doesn't use stderr to output errors

空扰寡人 提交于 2019-11-30 22:32:31
问题 I'm running the PHP CLI through a NSTask in MacOS, but this question is more about the CLI itself. I'm listening to the stderr pipe, but nothing is output there no matter what file I try to run: If the file type is not a plain text, stdout sets to ? . If the file is a php script with errors, the error messages are still printed to stdout . Is there a switch to the interpreter to handle errors through stderr ? Do I have an option to detect errors other than parsing stdout ? 回答1: The display

How to run NSTask with multiple commands

三世轮回 提交于 2019-11-30 10:15:06
I'm trying to make a NSTask running a command like this: ps -clx | grep 'Finder' | awk '{print $2}' Here is my method - (void) processByName:(NSString*)name { NSTask *task1 = [[NSTask alloc] init]; NSPipe *pipe1 = [NSPipe pipe]; [task1 waitUntilExit]; [task1 setLaunchPath: @"/bin/ps"]; [task1 setArguments: [NSArray arrayWithObjects: @"-clx", nil]]; [task1 setStandardOutput: pipe1]; NSTask *task2 = [[NSTask alloc] init]; NSPipe *pipe2 = [NSPipe pipe]; [task2 setLaunchPath: @"/usr/bin/grep"]; [task2 setArguments: [NSArray arrayWithObjects: @"'Finder'", nil]]; [task2 setStandardInput:pipe1];