如何列出 Mac 上 Python 中所有工作区的所有 windows?
How to list all windows from all workspaces in Python on Mac?
以下 Python 2 代码打印当前工作区中所有 windows 的列表:
#!/usr/bin/python
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID):
print("%s - %s" % (window['kCGWindowOwnerName'], window.get('kCGWindowName', u'Unknown').encode('ascii','ignore')))
虽然它不打印全屏应用程序(因为它在另一个工作区中)。
如何修改上述脚本以列出所有桌面的所有 windows?
以下脚本应 return window 任何 desktop/workspace/display 信息,全屏和详细信息(坐标、pid、标题等):
#!/usr/bin/python
import Quartz
import time
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListExcludeDesktopElements, kCGNullWindowID
from Foundation import NSSet, NSMutableSet
def windowList(wl):
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') # remove 'encode' for Python 3.x
wl1 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
print('Move target window (or ignore)\n')
time.sleep(5)
print('PID'.rjust(7) + ' ' + 'WinID'.rjust(5) + ' ' + 'x,y,w,h'.ljust(21) + ' ' + '\t[Title] SubTitle')
print('-'.rjust(7,'-') + ' ' + '-'.rjust(5,'-') + ' ' + '-'.ljust(21,'-') + ' ' + '\t-------------------------------------------')
wl2 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
w = NSMutableSet.setWithArray_(wl1)
w.minusSet_(NSSet.setWithArray_(wl2))
wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
wl = sorted(wl, key=lambda k: k.valueForKey_('kCGWindowOwnerPID'))
windowList(wl)
print('\nDetailed window information: {0}\n'.format(w))
这里的关键是对 CGWindowListCopyWindowInfo
的第一个参数使用正确的选项。所以除了使用 optionOnScreenOnly
property (which list all windows that are currently onscreen), excludeDesktopElements
之外还需要添加 属性
excludeDesktopElements
: Exclude any windows from the list that are elements of the desktop, including the background picture and desktop icons.
例如
list = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID)
或者所有windows,也可以使用kCGWindowListOptionAll
属性。
kCGWindowListOptionAll
: List all windows, including both onscreen and offscreen windows. When retrieving a list with this option, the relativeToWindow
parameter should be set to kCGNullWindowID
.
对于其他属性,请检查 CGWindow.h
in CoreGraphics
。
所以原代码可以改成:
#!/usr/bin/python
# Prints list of all windows.
# See:
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID):
print("%s - %s" % (window['kCGWindowOwnerName'], window.get('kCGWindowName', u'Unknown').encode('ascii','ignore')))
以下 Python 2 代码打印当前工作区中所有 windows 的列表:
#!/usr/bin/python
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID):
print("%s - %s" % (window['kCGWindowOwnerName'], window.get('kCGWindowName', u'Unknown').encode('ascii','ignore')))
虽然它不打印全屏应用程序(因为它在另一个工作区中)。
如何修改上述脚本以列出所有桌面的所有 windows?
以下脚本应 return window 任何 desktop/workspace/display 信息,全屏和详细信息(坐标、pid、标题等):
#!/usr/bin/python
import Quartz
import time
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListExcludeDesktopElements, kCGNullWindowID
from Foundation import NSSet, NSMutableSet
def windowList(wl):
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') # remove 'encode' for Python 3.x
wl1 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
print('Move target window (or ignore)\n')
time.sleep(5)
print('PID'.rjust(7) + ' ' + 'WinID'.rjust(5) + ' ' + 'x,y,w,h'.ljust(21) + ' ' + '\t[Title] SubTitle')
print('-'.rjust(7,'-') + ' ' + '-'.rjust(5,'-') + ' ' + '-'.ljust(21,'-') + ' ' + '\t-------------------------------------------')
wl2 = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements, kCGNullWindowID)
w = NSMutableSet.setWithArray_(wl1)
w.minusSet_(NSSet.setWithArray_(wl2))
wl = Quartz.CGWindowListCopyWindowInfo( Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID)
wl = sorted(wl, key=lambda k: k.valueForKey_('kCGWindowOwnerPID'))
windowList(wl)
print('\nDetailed window information: {0}\n'.format(w))
这里的关键是对 CGWindowListCopyWindowInfo
的第一个参数使用正确的选项。所以除了使用 optionOnScreenOnly
property (which list all windows that are currently onscreen), excludeDesktopElements
之外还需要添加 属性
excludeDesktopElements
: Exclude any windows from the list that are elements of the desktop, including the background picture and desktop icons.
例如
list = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID)
或者所有windows,也可以使用kCGWindowListOptionAll
属性。
kCGWindowListOptionAll
: List all windows, including both onscreen and offscreen windows. When retrieving a list with this option, therelativeToWindow
parameter should be set tokCGNullWindowID
.
对于其他属性,请检查 CGWindow.h
in CoreGraphics
。
所以原代码可以改成:
#!/usr/bin/python
# Prints list of all windows.
# See:
import Quartz
for window in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID):
print("%s - %s" % (window['kCGWindowOwnerName'], window.get('kCGWindowName', u'Unknown').encode('ascii','ignore')))