-
Notifications
You must be signed in to change notification settings - Fork 0
/
lswin.py
114 lines (95 loc) · 2.5 KB
/
lswin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'''
Lists all windows with their coordinates and dimensions in JSON format.
'''
import json
from argparse import ArgumentParser
import Quartz
from AppKit import (
NSWindow,
NSClosableWindowMask as closable,
NSResizableWindowMask as resizable,
NSMiniaturizableWindowMask as miniaturizable,
NSTitledWindowMask as titled
)
parser = ArgumentParser(description = 'Get list of windows.')
parser.add_argument(
"-f",
"--filter",
dest = "filter",
help = "filter windows by title"
)
parser.add_argument(
"-s",
"--filter-sub",
dest = "filterSub",
help = "filter windows by subtitle"
)
args = parser.parse_args()
wl = Quartz.CGWindowListCopyWindowInfo(
Quartz.kCGWindowListOptionAll,
Quartz.kCGNullWindowID
)
# print(wl)
wl = sorted(wl, key = lambda k: k.valueForKey_('kCGWindowOwnerPID'))
# print(wl)
def toDict(window):
"""
does stuff
"""
pid = int(window.valueForKey_('kCGWindowOwnerPID') or '?')
windowId = int(window.valueForKey_('kCGWindowNumber') or '?')
x = int(window.valueForKey_('kCGWindowBounds').valueForKey_('X'))
y = int(window.valueForKey_('kCGWindowBounds').valueForKey_('Y'))
width = int(window.valueForKey_('kCGWindowBounds').valueForKey_('Width'))
height = int(window.valueForKey_('kCGWindowBounds').valueForKey_('Height'))
title = (window.valueForKey_('kCGWindowOwnerName') or '')
subtitle = (
'' if window.valueForKey_('kCGWindowName') is None else
(window.valueForKey_('kCGWindowName') or '')
)
frameRectangle = {
"origin": {
"x": x,
"y": y
},
"size": {
"width": width,
"height": height
}
}
windowMask = (closable and resizable and miniaturizable and titled)
contentRectangle = {
k: window._asdict()
for k,
window in NSWindow.contentRectForFrameRect_styleMask_(
list(
Quartz.CGRectMakeWithDictionaryRepresentation(
window.valueForKey_('kCGWindowBounds'),
None
)
)[1],
windowMask
)._asdict().items()
}
contentRectangle["origin"]["y"] = (
contentRectangle["origin"]["y"] +
(frameRectangle["size"]["height"] - contentRectangle["size"]["height"])
)
windowDict = {
"pid": pid,
"windowId": windowId,
"title": title,
"subtitle": subtitle,
"contentRectangle": contentRectangle,
"frameRectangle": frameRectangle
}
return windowDict
windowList = list(map(toDict, wl))
if args.filter is not None:
windowList = list(filter(lambda d: d["title"] == args.filter, windowList))
if args.filterSub is not None:
windowList = list(
filter(lambda d: d["subtitle"] == args.filterSub,
windowList)
)
print(json.dumps(windowList))