0

Given the PID of a process, how can I check if it's a windowed process? I am looking for a solution on OSX.

Objective-C/ C solutions are wanted. I am on OSX Mountain Lion.

I am defining a windowed process as a process that can be seen when the "Windowed Processes" tab is selected in activity monitor. http://cl.ly/FeXR

Nandeep Mali
  • 4,456
  • 1
  • 25
  • 34
fdh
  • 5,256
  • 13
  • 58
  • 101
  • [This answer](http://stackoverflow.com/a/1888944/962089) explains a process in raw win32 where you check each running window to see if it belongs to the process you want. Perhaps that helps? – chris Sep 06 '12 at 18:28
  • @chris Thanks but I am looking for a solution for OSX in Objective-C or C. – fdh Sep 06 '12 at 18:31
  • For whatever reason, despite the numerous times you said OSX, it never registered in my brain. Apologies. Perhaps there's a similar method for OSX. – chris Sep 06 '12 at 18:40
  • @shellter You can't be serious? Did you take the time to *read* the other question? That question asks what defining characteristics applications with icons in Activity Monitor have in common. This question asks how to check if a process is a windowed process. – fdh Sep 06 '12 at 22:59

1 Answers1

2
[NSWorkspace runningApplications] 

This returns all the user applications in a NSArray as instances of NSRunningApplication which has a property processIdentifier.

Or you can use runningApplicationWithProcessIdentifier: directly to get the application with a specified PID.

Here is the sample code: http://developer.apple.com/library/Mac/#samplecode/AppList/Introduction/Intro.html

Through the activationPolicy property of the NSRunningApplication you could get the Windowed state. I modified the AppList code to make it show only the windowed applications as shown below. This matches what the activity monitor shows.

enter image description here

In particular, you are looking for the NSApplicationActivationPolicyRegular.

Note the restrictions however. This only works for user owned processes.

All other methods are deprecated and may not work in future. Methods like GetBSDProcessList and Carbon's GetProcessInformation are not recommended anymore.

Nandeep Mali
  • 4,456
  • 1
  • 25
  • 34
  • I don't see how this answers my question. You have simply provided a way to retrieve all user owned processes; the only mention of checking if a process is windowed doesn't answer the question in any way. – fdh Sep 06 '12 at 21:21
  • Have you even tried the code by yourself and read the documentation that I had pointed to? Anyway, I have updated my answer to reflect how to do that. – Nandeep Mali Sep 07 '12 at 08:32
  • The edit answer was very helpful! It works perfectly. Thank You for the help! – fdh Sep 08 '12 at 01:08