我做了一个名为 lswin
$ python lswin.py
PID WinID x,y,w,h [Title] SubTitle
------- ----- --------------------- -------------------------------------------
169 1956 {0,-38,1280,25 } [Window Server] Backstop Menubar
169 1955 {0,-60,1280,22 } [Window Server] Menubar
169 396 {0,-38,1280,25 } [Window Server] Backstop Menubar
169 395 {0,-60,1280,22 } [Window Server] Menubar
169 6 {0,0,0,0 } [Window Server] Cursor
169 4 {0,22,1280,25 } [Window Server] Backstop Menubar
169 3 {0,0,1280,22 } [Window Server] Menubar
169 2 {0,0,1280,800 } [Window Server] Desktop
262 404 {0,-38,1280,38 } [Google Chrome]
262 393 {0,0,1280,800 } [Google Chrome]
262 380 {100,100,1,1 } [Google Chrome] Focus Proxy
... ...
然后,您可以使用grep查找窗口的pid。
这是脚本的源代码:
#!/usr/bin/env python
import Quartz
#wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID)
wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
wl = sorted(wl, key=lambda k: k.valueForKey_('kCGWindowOwnerPID'))
#print wl
print 'PID'.rjust(7) + ' ' + 'WinID'.rjust(5) + ' ' + 'x,y,w,h'.ljust(21) + ' ' + '\t[Title] SubTitle'
print '-'.rjust(7,'-') + ' ' + '-'.rjust(5,'-') + ' ' + '-'.ljust(21,'-') + ' ' + '\t-------------------------------------------'
for v in wl:
print ( \
str(v.valueForKey_('kCGWindowOwnerPID') or '?').rjust(7) + \
' ' + str(v.valueForKey_('kCGWindowNumber') or '?').rjust(5) + \
' {' + ('' if v.valueForKey_('kCGWindowBounds') is None else \
( \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('X'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Y'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Width'))) + ',' + \
str(int(v.valueForKey_('kCGWindowBounds').valueForKey_('Height'))) \
) \
).ljust(21) + \
'}' + \
'\t[' + ((v.valueForKey_('kCGWindowOwnerName') or '') + ']') + \
('' if v.valueForKey_('kCGWindowName') is None else (' ' + v.valueForKey_('kCGWindowName') or '')) \
).encode('utf8')