3

I am getting the list of running applications in Cocoa with the following code:

for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
   MNSLog(@"%@",[app localizedName]);
}

However an application I started from a terminal session is not appearing in the list ('Terminal' is well appearing). The application was started from the same user which is executing the cocoa code.

Is my launched application under Terminal ? And in such a case how can I find its name and arguments ?

Running ps in another terminal session show my process properly.

Wain
  • 118,658
  • 15
  • 128
  • 151
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • Does your app use the window server? – Wain Nov 17 '13 at 16:28
  • Follow this link http://stackoverflow.com/questions/1973554/how-do-i-find-information-on-other-applications-running-on-the-computer-on-mac-c – Hussain Shabbir Nov 17 '13 at 16:36
  • Processes you start from Terminal that don't have UIs or otherwise contact the window server are not "applications", for future reference — they are just processes. Apple documentation also sometimes uses the word "tool". I think some people are getting confused because of this. When you're dealing with things at this level, generally Cocoa isn't going to help you; you need to use the lower-level BSD (or even Mach, though only as a last resort) APIs. – Nicholas Riley Nov 17 '13 at 21:26

2 Answers2

2

Use an NSTask to execute the ps Shell command. You can check the ps man page to determine which arguments you want to pass it based on the information you want to get back. Use NSPipe and NSFileHandle to get the results from the task.

If you want to do some filtering you can pipe the ps output through grep before your app picks up the result.

For your first question, I think NSWorkspace can only see apps that use the window server so you will only see Terminal, not the executables that it is running internally.

Wain
  • 118,658
  • 15
  • 128
  • 151
2

You can use sysctl or ps command to get a list of all BSD processes. Have look at unable to detect application running with another user

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144