Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > displaying stdout in NSTextView

displaying stdout in NSTextView
Thread Tools
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 3, 2005, 10:40 PM
 
I'm finding Cocoa's NSFileHandle a little difficult to come to grips with. I guess I'm just thinking the wrong way, or something.

Anyhow, can anyone give me some idea of how to either redirect stdout (of my App's process) and place the text in an NSTextView, or otherwise, how to read from the existing stdout and place the text in an NSTextView.

I'm sure this has got to be very simple, but I just can't figure it out.
     
Samad
Fresh-Faced Recruit
Join Date: Nov 2001
Location: home
Status: Offline
Reply With Quote
May 6, 2005, 12:46 AM
 
Here are some thoughts off the top of my head:
1) Close stdout (id = 1) and open it up to another file with an id = 1. Read that file to get stdout. I'm not too sure how to do this but it might be possible.
2) Have another process read from your program's output by a pipe, then send that back to your program. This is guaranteed to work, but requires some work.

If you want to read the stdout of another process, you'll have to use NSPipe (which is just a wrapper class for the Posix pipe() call).
     
Samad
Fresh-Faced Recruit
Join Date: Nov 2001
Location: home
Status: Offline
Reply With Quote
May 6, 2005, 12:55 AM
 
Here's an example on reading the output of a program:

//Create a task
NSTask *task = [[NSTask alloc] init];

// Create a pipe for communicating with task
NSPipe *pipe = [[NSPipe alloc] init];

// Create a file handle to read from the pipe, because pipes are just file handles
NSFileHandle *fh = [pipe fileHandleForReading];

// Assign a launch path to the task
[task setLaunchPath: @"/bin/ls"];

// Assign the arguments to the task
[task setArguments: [NSArray arrayWithObject @"/"]];

// Assign the stdout of the task to the pipe
[task setStandardOutput: pipe];

// Launch the task; this method does not return until the process has ended
[task launch];

// Get the data from the file handle
NSData *stdoutData = [fh availableData];

// Display stdoutData
NSLog(@"%s", [stdoutData bytes]);
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 6, 2005, 01:03 AM
 
That makes sense, but what about stdout for something other than an NSTask? I want to capture stdout for my entire application (ie, I want to be able to capture NSLog output as well).
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 8, 2005, 10:44 PM
 
Just for reference I've got this all working now, and solved some problems along the way:

calling [fh availableData] is best done after receiving a notification that there is available data to be read (otherwise the thread blocks)

need to register for this notification before receiving data, and in the callback, if you want to receive more data
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:28 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,