private
Task: NSTask;
LPipeInput,
LPipeOutput,
LPipeError: NSPipe;
// ConsoleError,
// ConsoleInput,
ConsoleOutput : NSData;
OutputText: string;
procedure Exec(FileName: PWideChar);
const
kLineFeed = #10;
procedure TMainForm.CheckForDataButtonClick(Sender: TObject);
begin
Assert(Task <> nil, 'Task has not been created.');
ConsoleOutput := LPipeOutput.fileHandleForReading.availableData;
OutputText := StringOf(BytesOf(ConsoleOutput.bytes, ConsoleOutput.length));
// Memo1.Text.Insert(Memo1.Text.Length, OutPutText)
Memo1.Text := OutputText;
end;
procedure TMainForm.Exec(FileName: PWideChar);
begin
Task := TNSTask.Wrap(TNSTask.Wrap(TNSTask.OCClass.alloc).init);
Task.setLaunchPath(StrToNSStr(FileName));
Task.setStandardOutput((LPipeOutput as ILocalObject).GetObjectID);
Task.setStandardError((LPipeError as ILocalObject).GetObjectID);
Task.setStandardInput((LPipeInput as ILocalObject).GetObjectID);
Task.launch;
// Task.waitUntilExit; This child process is supposed to keep running.
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
LPipeInput := TNSPipe.Create;
LPipeOutput := TNSPipe.Create;
LPipeError := TNSPipe.Create;
Task := nil;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if (Task <> nil)
then
begin
Task.release;
Task := nil;
end;
LPipeInput.release;
LPipeInput := nil;
LPipeOutput.release;
LPipeOutput := nil;
LPipeError.release;
LPipeError := nil;
end;
procedure TMainForm.SendButtonClick(Sender: TObject);
var
a: NSData;
str: NSString;
begin
Str := StrToNSStr(Edit1.Text + kLineFeed);
a := str.dataUsingEncoding(NSUTF8StringEncoding);
LPipeInput.fileHandleForWriting.writeData(a);
end;
procedure TMainForm.RunEngineButtonClick(Sender: TObject);
begin
Exec('/Users/michaelleahy/PAServer/scratch-dir/Mikes Mini Mac/Stockfish/stockfish-5-64');
end;
Head over and check out the full code snippet for reading and writing from and to a command line pipe on Mac OSX in Delphi XE7 Firemonkey.