Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:25:43+00:00 2026-06-17T10:25:43+00:00

I’m trying to use the top command in MacOS X to determine which app

  • 0

I’m trying to use the “top” command in MacOS X to determine which app is using the resources.
When I do:

top -stats “pid,command”

the command column is truncated, if the process name is too long.

if you look at the activity monitor, the process name is shown properly (with full name) + icon. My questions are:

  1. how to get the full process name?
  2. sometimes the app icon show next to the process name, is there anyway to do the similar thing using objective-c? should I simply navigate to the app contents folder and grab the icns image?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:25:44+00:00Added an answer on June 17, 2026 at 10:25 am

    First, if you’re trying to get the data programmatically, driving top is almost definitely not what you want to do.

    But, to answer your direct questions:

    how to get the full process name?

    There is no way to control the truncation of commands. You can use the -ncols parameter to set the width of the output for non-interactive output, but that doesn’t stop top from truncating if it wants to.

    sometimes the app icon show next to the process name, is there anyway to do the similar thing using objective-c? should I simply navigate to the app contents folder and grab the icns image?

    No. How would you deal with apps that have multiple .icns files, e.g., for document icons? (Try it with iTunes, for example. If you pick the first .icns, you get the AIFF document icon; if you pick the last, you get the internal-use recent TV shows icon.)

    The right way to do it is to get the NSBundle for the application, then do something like this:

    NSString *iconFile = [bundle objectForInfoDictionaryKey:@"CFBundleIconFile"];
    if (iconFile) {
        NSString *iconPath = [bundle pathForResource:iconFile ofType:@"icns"];
        // load and display the icon
    }
    

    So, how do you actually want to do this, if not by driving top?

    Well, what you’re asking for is actually not a well-defined thing. OS X has four different notions of task/process/program/application that don’t correspond 1-to-1, and that makes life difficult if you want to write a mashup of two programs that use different notions—e.g., top deals in BSD processes, while Activity Monitor deals in OS X applications.

    If what you actually want is the same list top uses, it’s open source, so you can read it and do the same thing it does.

    But the simplest way to get the list of BSD processes is probably the interfaces in libproc.h, in particular proc_listallpids and proc_pidinfo. For example:

    int dump_proc_names() {
      int buf[16384];
      int count = proc_listallpids(&buf, 16384*sizeof(int));
      for (int i = 0; i != count; ++i) {
        int pid = buf[i];
        char path[MAXPATHLEN+1] = {0};
        int ret = proc_pidinfo(pid, PROC_PIDPATHINFO, 0,
                               &path, sizeof(path));
        if (ret < 0) {
          printf("%d: error %s (%d)\n", pid, strerror(errno), errno);
        } else {
          printf("%d: %s\n", pid, path);
        }
      }
    }
    

    Obviously in real code you’re going to want to allocate the buffer dynamically, return the values instead of just dumping them, get more than just the paths, etc. But this is enough to give you the basic idea. (When you go to get additional information, be aware that you if you ask for any struct, you will get an EPERM error unless you have rights to see every member of that struct. So, don’t go asking for PROC_PIDTASKALLINFO if you only want PROC_PIDT_SHORTBSDINFO.


    Anyway, since this API deals with BSD processes (and Mach tasks), not applications, it won’t directly help you get at the NSBundle you want to provide Activity Monitor-style features.

    There is no way to do this that’s entirely correct, but you can probably get away with something like this:

    NSString *path = processPath;
    while (path && ![path isEqualTo:@"/"]) {
        NSBundle *bundle = [NSBundle bundleWithPath:path];
        if (bundle) {
            if ([bundle executablePath != processPath]) return nil;
            return bundle;
        }
        path = [path stringByDeletingLastPathComponent];
    }
    

    There are probably alternative ways to do this, each with different tradeoffs. For example, using -[NSWorkspace runningApplications], storing the results in a dictionary mapping the bundle executable path to the bundle, and using that to look up each process is simple, but it only seems to be useful for applications owned by the current user (and probably in the current session). On the other hand, enumerating all bundles on the system, or asking Spotlight, or similar would probably be too slow to do on the fly, but would go out of date if you cached them on first run.


    Another option, in place of libproc, is to use libtop.

    Unfortunately, Apple doesn’t provide it. They do have a libtop implementation, which they use for their top tool, but it’s actually embedded in the source to top and not available from outside. You can find the source (at the link above) and embed it into your program the same way top itself does.

    Alternatively, both GNU and BSD process utilities have Mac ports (although knowing which name to use with Homebrew/MacPorts/Google search isn’t always easy…), so you could build one of those and use it.

    However, unless you’re trying to write cross-platform software (or already know how to write this code for linux or FreeBSD or whatever), I think that just adds extra complexity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using Paperclip to handle profile photo uploads in my app. They upload
I am writing an app for my school newspaper, which is run completely online
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.