title: Execute terminal command top from a Cocoa app
body: <p>I want to execute top and get the result in order to know how much memory is used by a process.</p>
  
  <p>First, I want to execute a terminal command. I use code from this question
  <a href="https://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app/696942#696942">Execute a terminal command from a Cocoa app</a></p>
  
  <p>I tried with uptime and it worked flawlessly. </p>
  
  <p>But, when I tried to use top instead of uptime, i get the following error:</p>
  
  <p><strong>Error opening terminal: unknown.</strong></p>
  
  <p>Or, top is in /usr/bin/top, I don't know why it refuses to use it.</p>
  
  <p>Here is my code:</p>
  
  <pre><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];
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>Thanks.</p>