2017-05-28 56 views
2

當前工作區中的所有窗口的下面的Python 2代碼打印清單:如何在Mac上的Python中列出所有工作區中的所有窗口?

#!/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'))) 

雖然不打印這是在全屏幕應用程序(因爲它是在另一個工作區)。

如何修改上述腳本以列出所有桌面上的所有窗口?

回答

2

下面的腳本應該返回任何桌面/工作區/顯示器,全屏,並詳細信息窗口信息(座標,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') 

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)) 
1

的關鍵是這裏使用權選項CGWindowListCopyWindowInfo的第一個參數。因此,除了使用optionOnScreenOnly屬性(列出當前屏幕上的所有窗口)之外,還需要添加excludeDesktopElements屬性。

excludeDesktopElements:從列表中排除桌面元素(包括背景圖片和桌面圖標)中的任何窗口。

例如,

list = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements & kCGWindowListOptionOnScreenOnly, kCGNullWindowID) 

或者所有窗口,也可以用kCGWindowListOptionAll財產。

kCGWindowListOptionAll:列出所有窗口,包括屏​​幕上和屏幕外的窗口。使用此選項檢索列表時,應將relativeToWindow參數設置爲kCGNullWindowID

對於其他屬性,請檢查CGWindow.h in CoreGraphics


所以原代碼可以改爲:

#!/usr/bin/python 
# Prints list of all windows. 
# See: https://stackoverflow.com/q/44232433/55075 
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'))) 
相關問題