Friday, July 6, 2018

macos - How to identify which process is running which window in Mac OS X?


I’d like to know if it is possible to identify which process is responsible for creating/managing a window in Mac OS X.


For example, when multiple instances of an application are started, how can I get the process ID (PID) corresponding to one specific window? Or if there is a modal dialog window without a title, how can I get the PID of its owner?


I know in Windows it is possible using the Sysinternals Suite tool that provides a way to search for a library that is running with some data.


I’m looking for a mechanism similar to the one that appears in this blogpost.


In this case, using Sysinternals Suite (and Process Explorer), they found which DLL/program was using the webcam by searching for a DLL or substring (in this case, using the physical name of the device).


So is there any mechanism or program, or do you have any idea about how to search for something similar for Mac OS X? How I can identify which process has launched a window?


Answer



I've used the Python script. It isn't foolproof, but it works pretty well for me.


I won't repost the full script without permission, but here's a summary: It uses CGWindowListCopyWindowInfo, which is imported from Quartz, to collect window info from the system, then asks the user to move the desired window, then collects window info again, and shows info for the ones that changed. The info dumped includes the process ID, as kCGWindowOwnerPID.


Here is the code:


#!/usr/bin/env python
import time
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListExcludeDesktopElements, kCGNullWindowID
from Foundation import NSSet, NSMutableSet
wl1 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
print 'Move target window'
time.sleep(5)
wl2 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
w = NSMutableSet.setWithArray_(wl1)
w.minusSet_(NSSet.setWithArray_(wl2))
print '\nList of windows that moved:'
print w
print '\n'

The script prints information for the window that changed position within a 5 second interval. So the output looks like this:


List of windows that moved:
{(
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 217;
Width = 420;
X = 828;
Y = 213;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 8;
kCGWindowMemoryUsage = 406420;
kCGWindowName = "";
kCGWindowNumber = 77;
kCGWindowOwnerName = UserNotificationCenter;
kCGWindowOwnerPID = 481;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
}
)}

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...