I want to execute top and get the result in order to know how much memory is used by a process.
First, I want to execute a terminal command. I use code from this question Execute a terminal command from a Cocoa app
I tried with uptime and it worked flawlessly.
But, when I tried to use top instead of uptime, i get the following error:
Error opening terminal: unknown.
Or, top is in /usr/bin/top, I don't know why it refuses to use it.
Here is my code:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/top"];
/*NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"foo", @"bar.txt", nil];
[task setArguments: arguments];*/
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[label setStringValue: string];
[string release];
[task release];
Second question: the thing I am trying to do is to know how much memory is used by Google Chrome. Because it uses a process for each tab, it's difficult to know how much memeroy in total it uses.
Problem 1: top changes its value every second, how can I get a value? Problem 2: how can I search in the result of top in order to find only the processes I want (given that I know their names).
Thanks.