Skip to content

Conversation

yankeguo
Copy link

@yankeguo yankeguo commented Jul 7, 2025

Changes proposed in this pull request:

  • add -l argument to screencapture for macOS, allowing capture a window

@radarhere
Copy link
Member

Hi. Just for the record, how would a Python user get the window id?

@yankeguo
Copy link
Author

yankeguo commented Jul 7, 2025

here is a example script:

# pip install pyobjc
# pip install pyobjc-core
# pip install pyobjc-framework-quartz

import Quartz

windows = Quartz.CGWindowListCopyWindowInfo(
    Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID
)

for window in windows:
    window_id = window["kCGWindowNumber"]

    window_name = window["kCGWindowName"]

    window_x = window["kCGWindowBounds"]["X"]
    window_y = window["kCGWindowBounds"]["Y"]
    window_width = window["kCGWindowBounds"]["Width"]
    window_height = window["kCGWindowBounds"]["Height"]

    window_owner_name = window["kCGWindowOwnerName"]
    window_owner_pid = window["kCGWindowOwnerPID"]

user can determine which window id to use

there is also a tool:

https://github.com/smokris/GetWindowID

@radarhere radarhere changed the title feat(ImageGrab): enhance grab function to support window-based screenshot capturing on macOS Allow window id to be passed to ImageGrab.grab() on macOS Jul 7, 2025
im_resized = im.resize((right - left, bottom - top))
im.close()
return im_resized
# manual crop for windowed mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an interesting problem here.

Originally, we did just crop the image using Pillow. However, that changed in #6152, because #6144 reported that it wasn't accounting for scaling on retina displays.

So if you pick a specific window, how would we know whether it was scaled or not? It might be on a retina display or it might not. If this were Swift, I'd say we want the backingScaleFactor for the window. I'm not convinced AppleScript could easily get information about a window just from the id.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into whether there is any information about Retina in the EXIF data of the generated images.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems no good solution, may be we should just put this on hold.

However, using the -l parameter when capturing a window does have value, as it can create a complete screenshot of an obscured window compared to taking a full screen capture and then cropping it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of a less elegant method, using -R0,0,100,100 to sample an image and then calculate the ratio, but I cannot ensure that the window obtained with -l and the image sampled with -R are on the same display.

Another option is to behave like the screencapture command line, ignoring the bbox parameter when passing the window parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created yankeguo#2

One of the screencapture flags is

-o In window capture mode, do not capture the shadow of the window.

Excluding the shadow changes the size of the window. So if we take two screenshots of a window, we can measure the size of the shadow.

On a retina screen, the shadow is twice as wide as it is on a non-retina screen.

It is possible that a window doesn't have a shadow, but perhaps that can be considered an edge case.


:param window:
HWND, to capture a single window. Windows only.
Capture a single window. On Windows, this is a HWND. On macOS, it uses windowid.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's windowid? There's no other mention in the docs or code.

Copy link
Member

@radarhere radarhere Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit to describe it as a CGWindowID instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants