1

I would like to capture some state of a minimised (chat) application by reading it from a screenshot.

I know how to take screenshots of visible windows using screencapture but I do not know how to generalise this to minimised windows. This seems to be a working example for windows: Python Screenshot of inactive window PrintWindow + win32gui

Is there a MacOS equivalent?

edit: my question is not a duplicate of the question linked above, because I need more than a win32 equivalent, I need the screenshot functionality on top of it.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 1
    Possible duplicate of [What is an alternative to win32gui in python 2.7 for mac](https://stackoverflow.com/questions/29814634/what-is-an-alternative-to-win32gui-in-python-2-7-for-mac) – Constantin Guidon Jul 15 '19 at 15:09
  • 1
    @ConstantinGuidon Thank you, I used this snippet/example to build a solution to my problem. – Learning is a mess Jul 16 '19 at 09:58

1 Answers1

0

I created a minimal working example:

from Quartz import CGWindowListCopyWindowInfo, kCGNullWindowID, kCGWindowListOptionAll
import matplotlib.pyplot as plt
from PIL import Image
import os
from uuid import uuid4

gen_filename = lambda : str(uuid4())[-10:] + '.jpg'

def capture_window(window_name):
    window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID)
    for window in window_list:
        try:
            if window_name.lower() in window['kCGWindowName'].lower():
                filename = gen_filename()
                os.system('screencapture -l %s %s' %(window['kCGWindowNumber'], filename))
                img = Image.open(filename)
                plt.imshow(img)
                plt.xticks([])
                plt.yticks([])
                os.remove(filename)
                break
        except:
            pass
    else:
        raise Exception('Window %s not found.'%window_name)

It uses screencapture to save the window screenshot to a temporary file then load it back in Python.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71