Kalmat / PyWinCtl

Cross-Platform module to get info on and control windows on screen
Other
179 stars 19 forks source link

Get the width and height of the software window #68

Closed sgx-wb closed 1 year ago

sgx-wb commented 1 year ago

image

In macbook, I want to get the width, height and position of the software window, but the returned data type, python can not be taken directly, is there any way?

Kalmat commented 1 year ago

Hi!

The short answer is using PyWinCtl module, like this:

import pywinctl as pwc

for win in pwc.getWindowsWithTitle("Lark"):
    print("X:", win.left, "Y:", win.top, "WIDTH:", win.width, "HEIGHT:", win.height)

The long answer is:

  1. Do not rely on kCGWindowOwnerName, since it is not mandatory (see this). Even though you're interested only in "Lark" app, and it seems to fulfill this field by the moment, it is not guaranteed in the future.
  2. To access a value in a dictionary by its key, you have to do like this: lark["X"] or, as you were already doing with other values, like this: lark.get("X", None)

EDIT: Sorry I linked to the wrong page (kCGWindowName instead of kCGWindowOwnerName). kCGWindowOwnerName is mandatory, but you can not use it to address a single window in case the application launches several of them.

sgx-wb commented 1 year ago

@Kalmat Thank you very much for your reply. This method works.

Kalmat commented 1 year ago

You're welcome! Just curious... which method are you eventually using?

sgx-wb commented 1 year ago

@Kalmat It seems straightforward to obtain the value this way. lark["X"]