#This is how to get your OpenClaw to

1 messages ยท Page 1 of 1 (latest)

wind juniper
#

window_screenshot.py

import time
from pathlib import Path

import win32gui
import win32ui
import win32con
from PIL import Image

DEFAULT_OUT = Path(r"C:\Users\antdx\Downloads\last_screenshot.png")


def find_window(title: str):
    hwnd = win32gui.FindWindow(None, title)
    if hwnd:
        return hwnd
    # Fallback: partial match
    def enum_handler(h, results):
        if win32gui.IsWindowVisible(h):
            text = win32gui.GetWindowText(h)
            if title.lower() in text.lower():
                results.append(h)

    results = []
    win32gui.EnumWindows(enum_handler, results)
    return results[0] if results else None


def capture_window(hwnd, out_path: Path):
    # Bring to foreground (best effort)
    win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
    try:
        win32gui.SetForegroundWindow(hwnd)
    except Exception:
        pass
    time.sleep(0.2)

    left, top, right, bottom = win32gui.GetClientRect(hwnd)
    left_top = win32gui.ClientToScreen(hwnd, (left, top))
    right_bottom = win32gui.ClientToScreen(hwnd, (right, bottom))
    left, top = left_top
    right, bottom = right_bottom

    width = right - left
    height = bottom - top

    hwin = win32gui.GetDesktopWindow()
    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    bmpinfo = bmp.GetInfo()
    bmpstr = bmp.GetBitmapBits(True)
    img = Image.frombuffer(
        "RGB",
        (bmpinfo["bmWidth"], bmpinfo["bmHeight"]),
        bmpstr, "raw", "BGRX", 0, 1
    )
    img.save(out_path)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())


def capture_fullscreen(out_path: Path):
    hwin = win32gui.GetDesktopWindow()
    left, top, right, bottom = win32gui.GetWindowRect(hwin)
    width = right - left
    height = bottom - top

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    bmpinfo = bmp.GetInfo()
    bmpstr = bmp.GetBitmapBits(True)
    img = Image.frombuffer(
        "RGB",
        (bmpinfo["bmWidth"], bmpinfo["bmHeight"]),
        bmpstr, "raw", "BGRX", 0, 1
    )
    img.save(out_path)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())


def main():
    args = [a for a in sys.argv[1:] if a.strip()]
    fullscreen = False
    if args and args[0] == "--fullscreen":
        fullscreen = True
        args = args[1:]

    if fullscreen:
        out_path = DEFAULT_OUT
        capture_fullscreen(out_path)
        print(f"Saved: {out_path}")
        return

    if args:
        title = " ".join(args).strip()
    else:
        title = input("Window title (full or partial): ").strip()

    if not title:
        print("No title provided.")
        sys.exit(1)

    hwnd = find_window(title)
    if not hwnd:
        print(f"Window not found: {title}")
        sys.exit(2)

    out_path = DEFAULT_OUT
    capture_window(hwnd, out_path)
    print(f"Saved: {out_path}")


if __name__ == "__main__":
    main()```
#

I'm turning it into a 'skill'.

#
name: window-screenshot
description: Capture screenshots of specific Windows application windows by title using a bundled Python script. Use when the user asks for a screenshot of a particular window (e.g., Discord, a Python GUI app) or wants a reusable window-capture utility.
---

# Window Screenshot

## Overview
Capture a single window by title (exact or partial match) or the full screen, and save to a PNG. Uses a bundled Python script.

## Quick Start
1) Ensure dependencies are installed:
   - `pywin32`
   - `Pillow`

2) Run the script with the window title:
python scripts/window_screenshot.py "Window Title Here"


For full-screen:
python scripts/window_screenshot.py --fullscreen


3) Output saves to:
`C:\Users\antdx\Downloads\last_screenshot.png`

## Behavior
- Matches **exact title** first, then **partial title** (case-insensitive).
- If `SetForegroundWindow` fails, it still captures the window.
- Captures the **client area** of the window.
- `--fullscreen` captures the entire desktop.

## Troubleshooting
- If no match: list open windows and re-run with exact title text.
- If output looks wrong: resize/restore the window and retry.```