-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.py
51 lines (43 loc) · 1.5 KB
/
screen.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
import time
import AppKit as appkit
import pyscreenshot
import Quartz as quartz
def _get_active_window():
workspace = appkit.NSWorkspace.sharedWorkspace()
active_apps = workspace.runningApplications()
for app in active_apps:
name = app.localizedName()
if name and name.startswith('Hexcells'): # TODO: this is a stopgap
# if app.isActive():
options = quartz.kCGWindowListOptionOnScreenOnly
window_list = quartz.CGWindowListCopyWindowInfo(options, quartz.kCGNullWindowID)
for window in window_list:
if window['kCGWindowOwnerName'] == app.localizedName():
return window
def grab_game_screen():
window = None
while True:
window = _get_active_window()
name = window['kCGWindowOwnerName']
if name.startswith('Hexcells'):
break
print("Please make Hexcells the active window ({!r}).".format(name))
time.sleep(3)
continue
top_bar_height = 20 # TODO: less magic pls
dims = window['kCGWindowBounds']
bbox = (
int(dims['X']),
int(dims['Y']) + top_bar_height,
int(dims['Width']),
int(dims['Height']) - top_bar_height,
)
return pyscreenshot.grab(
bbox=bbox,
# importing Quartz before trying to screenshot causes a segfault without this flag. ᖍ(シ)ᖌ
childprocess=False,
), (bbox[0], bbox[1])
if __name__ == '__main__':
time.sleep(2)
im, _ = grab_game_screen()
im.show()