appsnap 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- appsnap/__init__.py +15 -0
- appsnap/__main__.py +292 -0
- appsnap/capture.py +137 -0
- appsnap/windows.py +115 -0
- appsnap-0.2.0.dist-info/METADATA +281 -0
- appsnap-0.2.0.dist-info/RECORD +9 -0
- appsnap-0.2.0.dist-info/WHEEL +4 -0
- appsnap-0.2.0.dist-info/entry_points.txt +2 -0
- appsnap-0.2.0.dist-info/licenses/LICENSE +21 -0
appsnap/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""appsnap - Fast Windows screenshot tool for AI coding agents."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.2.0"
|
|
4
|
+
__author__ = "appsnap contributors"
|
|
5
|
+
__license__ = "MIT"
|
|
6
|
+
|
|
7
|
+
from appsnap.windows import find_all_windows, find_window
|
|
8
|
+
from appsnap.capture import capture_window as capture_window_screenshot
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"find_all_windows",
|
|
12
|
+
"find_window",
|
|
13
|
+
"capture_window_screenshot",
|
|
14
|
+
"__version__",
|
|
15
|
+
]
|
appsnap/__main__.py
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"""Main CLI entry point for appsnap."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
import tempfile
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import NoReturn, Optional
|
|
10
|
+
|
|
11
|
+
from appsnap import __version__
|
|
12
|
+
from appsnap.capture import capture_window as capture_window_screenshot, validate_output_path
|
|
13
|
+
from appsnap.windows import find_window, get_window_list_formatted, find_all_windows
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def list_windows() -> None:
|
|
17
|
+
"""List all capturable windows and exit."""
|
|
18
|
+
windows = get_window_list_formatted()
|
|
19
|
+
|
|
20
|
+
if not windows:
|
|
21
|
+
print("No windows found.", file=sys.stderr)
|
|
22
|
+
sys.exit(1)
|
|
23
|
+
|
|
24
|
+
print(f"Found {len(windows)} window(s):\n")
|
|
25
|
+
for title in windows:
|
|
26
|
+
print(f" • {title}")
|
|
27
|
+
|
|
28
|
+
sys.exit(0)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def generate_temp_path() -> str:
|
|
32
|
+
"""
|
|
33
|
+
Generate a timestamped filename in the system temp directory.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
Full path to temp file like: /tmp/appsnap_YYYYMMDD_HHMMSS.png
|
|
37
|
+
"""
|
|
38
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
39
|
+
filename = f"appsnap_{timestamp}.png"
|
|
40
|
+
temp_dir = Path(tempfile.gettempdir()) / "appsnap"
|
|
41
|
+
temp_dir.mkdir(exist_ok=True)
|
|
42
|
+
return str(temp_dir / filename)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def sanitize_filename(title: str) -> str:
|
|
46
|
+
"""
|
|
47
|
+
Sanitize window title for use as filename.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
title: Window title to sanitize
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
Safe filename string
|
|
54
|
+
"""
|
|
55
|
+
# Replace invalid filename characters
|
|
56
|
+
invalid_chars = '<>:"/\\|?*'
|
|
57
|
+
for char in invalid_chars:
|
|
58
|
+
title = title.replace(char, '_')
|
|
59
|
+
# Limit length and strip whitespace
|
|
60
|
+
title = title.strip()[:100]
|
|
61
|
+
return title if title else "untitled"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def capture_all_windows(output_dir: str, json_output: bool) -> None:
|
|
65
|
+
"""
|
|
66
|
+
Capture screenshots of all windows to a directory.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
output_dir: Directory to save all screenshots
|
|
70
|
+
json_output: Whether to output JSON summary
|
|
71
|
+
"""
|
|
72
|
+
windows = find_all_windows()
|
|
73
|
+
|
|
74
|
+
if not windows:
|
|
75
|
+
print("No windows found.", file=sys.stderr)
|
|
76
|
+
sys.exit(1)
|
|
77
|
+
|
|
78
|
+
# Create output directory
|
|
79
|
+
output_path = Path(output_dir)
|
|
80
|
+
output_path.mkdir(parents=True, exist_ok=True)
|
|
81
|
+
|
|
82
|
+
# Track duplicate titles for numbering
|
|
83
|
+
title_counts = {}
|
|
84
|
+
results = []
|
|
85
|
+
successful = 0
|
|
86
|
+
failed = 0
|
|
87
|
+
|
|
88
|
+
print(
|
|
89
|
+
f"Capturing {len(windows)} window(s) to {output_path.resolve()}...\n")
|
|
90
|
+
|
|
91
|
+
for window in windows:
|
|
92
|
+
title = window["title"]
|
|
93
|
+
handle = window["handle"]
|
|
94
|
+
|
|
95
|
+
# Handle duplicate titles with counter
|
|
96
|
+
if title in title_counts:
|
|
97
|
+
title_counts[title] += 1
|
|
98
|
+
safe_title = sanitize_filename(title)
|
|
99
|
+
filename = f"{safe_title}_{title_counts[title]}.png"
|
|
100
|
+
else:
|
|
101
|
+
title_counts[title] = 1
|
|
102
|
+
safe_title = sanitize_filename(title)
|
|
103
|
+
filename = f"{safe_title}.png"
|
|
104
|
+
|
|
105
|
+
output_file = output_path / filename
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
capture_window_screenshot(handle, str(output_file))
|
|
109
|
+
print(f"[OK] {title}")
|
|
110
|
+
results.append({
|
|
111
|
+
"window": title,
|
|
112
|
+
"path": str(output_file.resolve()),
|
|
113
|
+
"status": "success"
|
|
114
|
+
})
|
|
115
|
+
successful += 1
|
|
116
|
+
except Exception as e:
|
|
117
|
+
print(f"[FAIL] {title}: {e}", file=sys.stderr)
|
|
118
|
+
results.append({
|
|
119
|
+
"window": title,
|
|
120
|
+
"status": "failed",
|
|
121
|
+
"error": str(e)
|
|
122
|
+
})
|
|
123
|
+
failed += 1
|
|
124
|
+
|
|
125
|
+
# Summary
|
|
126
|
+
print(f"\nComplete: {successful} successful, {failed} failed")
|
|
127
|
+
print(f"Screenshots saved to: {output_path.resolve()}")
|
|
128
|
+
|
|
129
|
+
# JSON output if requested
|
|
130
|
+
if json_output:
|
|
131
|
+
summary = {
|
|
132
|
+
"output_dir": str(output_path.resolve()),
|
|
133
|
+
"total": len(windows),
|
|
134
|
+
"successful": successful,
|
|
135
|
+
"failed": failed,
|
|
136
|
+
"results": results
|
|
137
|
+
}
|
|
138
|
+
print(json.dumps(summary, indent=2))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def capture_window(
|
|
142
|
+
window_name: str, output_path: Optional[str], threshold: int, json_output: bool
|
|
143
|
+
) -> None:
|
|
144
|
+
"""
|
|
145
|
+
Capture a window screenshot and output the result.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
window_name: Window title to search for
|
|
149
|
+
output_path: Custom output path, or None for temp directory
|
|
150
|
+
threshold: Fuzzy matching threshold (0-100)
|
|
151
|
+
json_output: Whether to output JSON format
|
|
152
|
+
"""
|
|
153
|
+
# Find the window
|
|
154
|
+
window = find_window(window_name, threshold=threshold)
|
|
155
|
+
|
|
156
|
+
if not window:
|
|
157
|
+
print(
|
|
158
|
+
f'Error: No window found matching "{window_name}" (threshold: {threshold})',
|
|
159
|
+
file=sys.stderr,
|
|
160
|
+
)
|
|
161
|
+
print("\nTip: Use 'appsnap --list' to see all available windows",
|
|
162
|
+
file=sys.stderr)
|
|
163
|
+
print(" Or try lowering the threshold with '--threshold 60'",
|
|
164
|
+
file=sys.stderr)
|
|
165
|
+
sys.exit(1)
|
|
166
|
+
|
|
167
|
+
# Determine output path
|
|
168
|
+
if output_path is None:
|
|
169
|
+
output_path = generate_temp_path()
|
|
170
|
+
else:
|
|
171
|
+
# Validate custom path
|
|
172
|
+
try:
|
|
173
|
+
validate_output_path(output_path)
|
|
174
|
+
except ValueError as e:
|
|
175
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
176
|
+
sys.exit(1)
|
|
177
|
+
|
|
178
|
+
# Capture the screenshot
|
|
179
|
+
try:
|
|
180
|
+
capture_window_screenshot(window["handle"], output_path)
|
|
181
|
+
except ValueError as e:
|
|
182
|
+
print(f"Error capturing screenshot: {e}", file=sys.stderr)
|
|
183
|
+
sys.exit(1)
|
|
184
|
+
except Exception as e:
|
|
185
|
+
print(f"Unexpected error: {e}", file=sys.stderr)
|
|
186
|
+
sys.exit(1)
|
|
187
|
+
|
|
188
|
+
# Output results
|
|
189
|
+
if json_output:
|
|
190
|
+
result = {
|
|
191
|
+
"path": str(Path(output_path).resolve()),
|
|
192
|
+
"window": window["title"],
|
|
193
|
+
"bbox": list(window["bbox"]),
|
|
194
|
+
}
|
|
195
|
+
print(json.dumps(result, indent=2))
|
|
196
|
+
else:
|
|
197
|
+
print(str(Path(output_path).resolve()))
|
|
198
|
+
if output_path.startswith(tempfile.gettempdir()):
|
|
199
|
+
print(f"Window: {window['title']}", file=sys.stderr)
|
|
200
|
+
print(f"Note: Temp files may be auto-deleted by the system",
|
|
201
|
+
file=sys.stderr)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def main() -> NoReturn:
|
|
205
|
+
"""Main CLI entry point."""
|
|
206
|
+
parser = argparse.ArgumentParser(
|
|
207
|
+
prog="appsnap",
|
|
208
|
+
description="Fast Windows screenshot tool for AI coding agents",
|
|
209
|
+
epilog='Example: appsnap "Visual Studio Code" --output screenshot.png',
|
|
210
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
parser.add_argument(
|
|
214
|
+
"window_name",
|
|
215
|
+
nargs="?",
|
|
216
|
+
help="Window title to search for (supports fuzzy matching)",
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
parser.add_argument(
|
|
220
|
+
"-l",
|
|
221
|
+
"--list",
|
|
222
|
+
action="store_true",
|
|
223
|
+
help="List all capturable windows and exit",
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
parser.add_argument(
|
|
227
|
+
"-a",
|
|
228
|
+
"--all",
|
|
229
|
+
metavar="DIR",
|
|
230
|
+
help="Capture all windows to specified directory",
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
parser.add_argument(
|
|
234
|
+
"-o",
|
|
235
|
+
"--output",
|
|
236
|
+
metavar="PATH",
|
|
237
|
+
help="Output file path (default: temp directory with timestamp)",
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
parser.add_argument(
|
|
241
|
+
"-t",
|
|
242
|
+
"--threshold",
|
|
243
|
+
type=int,
|
|
244
|
+
default=70,
|
|
245
|
+
metavar="N",
|
|
246
|
+
help="Fuzzy match threshold 0-100 (default: 70, lower = more lenient)",
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
parser.add_argument(
|
|
250
|
+
"-j",
|
|
251
|
+
"--json",
|
|
252
|
+
action="store_true",
|
|
253
|
+
help="Output result as JSON with path and metadata",
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
parser.add_argument(
|
|
257
|
+
"-v",
|
|
258
|
+
"--version",
|
|
259
|
+
action="version",
|
|
260
|
+
version=f"appsnap {__version__}",
|
|
261
|
+
help="Show version and exit",
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
args = parser.parse_args()
|
|
265
|
+
|
|
266
|
+
# Handle --list flag
|
|
267
|
+
if args.list:
|
|
268
|
+
list_windows()
|
|
269
|
+
|
|
270
|
+
# Handle --all flag
|
|
271
|
+
if args.all:
|
|
272
|
+
capture_all_windows(args.all, args.json)
|
|
273
|
+
sys.exit(0)
|
|
274
|
+
|
|
275
|
+
# Validate arguments
|
|
276
|
+
if not args.window_name:
|
|
277
|
+
parser.print_help()
|
|
278
|
+
sys.exit(1)
|
|
279
|
+
|
|
280
|
+
# Validate threshold range
|
|
281
|
+
if not 0 <= args.threshold <= 100:
|
|
282
|
+
print("Error: Threshold must be between 0 and 100", file=sys.stderr)
|
|
283
|
+
sys.exit(1)
|
|
284
|
+
|
|
285
|
+
# Capture window screenshot
|
|
286
|
+
capture_window(args.window_name, args.output, args.threshold, args.json)
|
|
287
|
+
|
|
288
|
+
sys.exit(0)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
if __name__ == "__main__":
|
|
292
|
+
main()
|
appsnap/capture.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Screenshot capture utilities using Win32 PrintWindow with fallback."""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
from ctypes import windll
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from PIL import Image, ImageGrab
|
|
9
|
+
import win32gui
|
|
10
|
+
import win32ui
|
|
11
|
+
import win32con
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def capture_window(hwnd: int, output_path: str) -> None:
|
|
15
|
+
"""
|
|
16
|
+
Capture a screenshot of a specific window by handle.
|
|
17
|
+
|
|
18
|
+
Tries Win32 PrintWindow API first for direct window content capture.
|
|
19
|
+
Falls back to screen region capture if PrintWindow fails (protected apps).
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
hwnd: Window handle (HWND) to capture
|
|
23
|
+
output_path: File path where screenshot will be saved (PNG format)
|
|
24
|
+
|
|
25
|
+
Raises:
|
|
26
|
+
ValueError: If window cannot be captured
|
|
27
|
+
OSError: If screenshot cannot be saved
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
>>> hwnd = 12345 # Window handle
|
|
31
|
+
>>> capture_window(hwnd, "screenshot.png")
|
|
32
|
+
"""
|
|
33
|
+
# Validate window is visible
|
|
34
|
+
if not win32gui.IsWindow(hwnd) or not win32gui.IsWindowVisible(hwnd):
|
|
35
|
+
raise ValueError("Window is not visible or invalid handle")
|
|
36
|
+
|
|
37
|
+
# Check if window is minimized
|
|
38
|
+
if win32gui.IsIconic(hwnd):
|
|
39
|
+
raise ValueError("Window is minimized - cannot capture")
|
|
40
|
+
|
|
41
|
+
# Get window dimensions
|
|
42
|
+
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
|
|
43
|
+
width = right - left
|
|
44
|
+
height = bottom - top
|
|
45
|
+
|
|
46
|
+
if width <= 0 or height <= 0:
|
|
47
|
+
raise ValueError(f"Invalid window dimensions: {width}x{height}")
|
|
48
|
+
|
|
49
|
+
# Ensure output directory exists
|
|
50
|
+
output_path_obj = Path(output_path)
|
|
51
|
+
output_path_obj.parent.mkdir(parents=True, exist_ok=True)
|
|
52
|
+
|
|
53
|
+
# Try PrintWindow first (works for most apps, even when partially occluded)
|
|
54
|
+
try:
|
|
55
|
+
hwndDC = win32gui.GetWindowDC(hwnd)
|
|
56
|
+
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
|
|
57
|
+
saveDC = mfcDC.CreateCompatibleDC()
|
|
58
|
+
|
|
59
|
+
saveBitMap = win32ui.CreateBitmap()
|
|
60
|
+
saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)
|
|
61
|
+
saveDC.SelectObject(saveBitMap)
|
|
62
|
+
|
|
63
|
+
# PW_RENDERFULLCONTENT (0x00000002)
|
|
64
|
+
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 2)
|
|
65
|
+
|
|
66
|
+
if result != 0:
|
|
67
|
+
# PrintWindow succeeded
|
|
68
|
+
bmpinfo = saveBitMap.GetInfo()
|
|
69
|
+
bmpstr = saveBitMap.GetBitmapBits(True)
|
|
70
|
+
img = Image.frombuffer(
|
|
71
|
+
'RGB',
|
|
72
|
+
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
|
|
73
|
+
bmpstr, 'raw', 'BGRX', 0, 1
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# Clean up
|
|
77
|
+
win32gui.DeleteObject(saveBitMap.GetHandle())
|
|
78
|
+
saveDC.DeleteDC()
|
|
79
|
+
mfcDC.DeleteDC()
|
|
80
|
+
win32gui.ReleaseDC(hwnd, hwndDC)
|
|
81
|
+
|
|
82
|
+
img.save(str(output_path_obj), "PNG")
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
# PrintWindow failed, clean up before fallback
|
|
86
|
+
win32gui.DeleteObject(saveBitMap.GetHandle())
|
|
87
|
+
saveDC.DeleteDC()
|
|
88
|
+
mfcDC.DeleteDC()
|
|
89
|
+
win32gui.ReleaseDC(hwnd, hwndDC)
|
|
90
|
+
|
|
91
|
+
except Exception:
|
|
92
|
+
pass # Fall through to ImageGrab fallback
|
|
93
|
+
|
|
94
|
+
# Fallback: Use PIL ImageGrab for screen region capture
|
|
95
|
+
# This works for protected windows but requires them to be visible
|
|
96
|
+
try:
|
|
97
|
+
# Try to bring window to foreground for better capture
|
|
98
|
+
try:
|
|
99
|
+
win32gui.SetForegroundWindow(hwnd)
|
|
100
|
+
time.sleep(0.05) # Brief pause for window to come to front
|
|
101
|
+
except:
|
|
102
|
+
pass # Some windows don't allow foreground activation
|
|
103
|
+
|
|
104
|
+
# Capture the screen region where the window is located
|
|
105
|
+
img = ImageGrab.grab(bbox=(left, top, right, bottom), all_screens=True)
|
|
106
|
+
|
|
107
|
+
if img is None or img.size[0] == 0 or img.size[1] == 0:
|
|
108
|
+
raise ValueError("Captured image is empty")
|
|
109
|
+
|
|
110
|
+
img.save(str(output_path_obj), "PNG")
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
except Exception as e:
|
|
114
|
+
raise ValueError(f"Failed to capture window: {e}")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def validate_output_path(path: str) -> None:
|
|
118
|
+
"""
|
|
119
|
+
Validate that an output path is usable.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
path: File path to validate
|
|
123
|
+
|
|
124
|
+
Raises:
|
|
125
|
+
ValueError: If path is invalid or points to a directory
|
|
126
|
+
"""
|
|
127
|
+
output_path = Path(path)
|
|
128
|
+
|
|
129
|
+
# Check if path points to an existing directory
|
|
130
|
+
if output_path.exists() and output_path.is_dir():
|
|
131
|
+
raise ValueError(f"Output path is a directory: {path}")
|
|
132
|
+
|
|
133
|
+
# Check if parent directory can be created/accessed
|
|
134
|
+
try:
|
|
135
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
136
|
+
except Exception as e:
|
|
137
|
+
raise ValueError(f"Cannot create parent directory for {path}: {e}")
|
appsnap/windows.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Window enumeration and finding utilities for Windows."""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
from typing import Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
import win32gui
|
|
7
|
+
from fuzzywuzzy import process
|
|
8
|
+
|
|
9
|
+
# DPI awareness setup (from Windows-MCP pattern)
|
|
10
|
+
PROCESS_PER_MONITOR_DPI_AWARE = 2
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def setup_dpi_awareness() -> None:
|
|
14
|
+
"""
|
|
15
|
+
Set up DPI awareness for accurate window coordinate retrieval.
|
|
16
|
+
|
|
17
|
+
Uses Per-Monitor DPI Awareness (v2) if available, falls back to basic DPI awareness.
|
|
18
|
+
"""
|
|
19
|
+
try:
|
|
20
|
+
ctypes.windll.shcore.SetProcessDpiAwareness(
|
|
21
|
+
PROCESS_PER_MONITOR_DPI_AWARE)
|
|
22
|
+
except Exception:
|
|
23
|
+
# Fallback for older Windows versions
|
|
24
|
+
ctypes.windll.user32.SetProcessDPIAware()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def find_all_windows() -> List[Dict[str, any]]:
|
|
28
|
+
"""
|
|
29
|
+
Enumerate all visible windows with titles.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
List of window dictionaries containing:
|
|
33
|
+
- handle: Window handle (HWND)
|
|
34
|
+
- title: Window title text
|
|
35
|
+
- bbox: Bounding box tuple (left, top, right, bottom)
|
|
36
|
+
"""
|
|
37
|
+
windows = []
|
|
38
|
+
|
|
39
|
+
def callback(hwnd, _):
|
|
40
|
+
if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd):
|
|
41
|
+
# Skip minimized windows
|
|
42
|
+
if win32gui.IsIconic(hwnd):
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
title = win32gui.GetWindowText(hwnd)
|
|
46
|
+
if title: # Only include windows with titles
|
|
47
|
+
try:
|
|
48
|
+
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
|
|
49
|
+
# Filter out windows with invalid dimensions
|
|
50
|
+
if right > left and bottom > top:
|
|
51
|
+
windows.append(
|
|
52
|
+
{
|
|
53
|
+
"handle": hwnd,
|
|
54
|
+
"title": title,
|
|
55
|
+
"bbox": (left, top, right, bottom),
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
except Exception:
|
|
59
|
+
# Skip windows that can't be queried
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
win32gui.EnumWindows(callback, None)
|
|
63
|
+
return windows
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def find_window(window_name: str, threshold: int = 70) -> Optional[Dict[str, any]]:
|
|
67
|
+
"""
|
|
68
|
+
Find a window by fuzzy name matching.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
window_name: Window title to search for (partial matches supported)
|
|
72
|
+
threshold: Fuzzy match threshold 0-100 (default 70).
|
|
73
|
+
Higher = stricter matching, lower = more lenient.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
Window dictionary with handle, title, and bbox, or None if not found.
|
|
77
|
+
|
|
78
|
+
Example:
|
|
79
|
+
>>> window = find_window("Visual Studio")
|
|
80
|
+
>>> if window:
|
|
81
|
+
... print(f"Found: {window['title']}")
|
|
82
|
+
... print(f"Bbox: {window['bbox']}")
|
|
83
|
+
"""
|
|
84
|
+
windows = find_all_windows()
|
|
85
|
+
if not windows:
|
|
86
|
+
return None
|
|
87
|
+
|
|
88
|
+
# Create mapping of titles to window data
|
|
89
|
+
window_titles = {w["title"]: w for w in windows}
|
|
90
|
+
|
|
91
|
+
# Fuzzy match against all window titles
|
|
92
|
+
match = process.extractOne(
|
|
93
|
+
window_name, list(window_titles.keys()), score_cutoff=threshold
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if match:
|
|
97
|
+
matched_title = match[0]
|
|
98
|
+
return window_titles[matched_title]
|
|
99
|
+
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def get_window_list_formatted() -> List[str]:
|
|
104
|
+
"""
|
|
105
|
+
Get a sorted list of all window titles for display.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
Sorted list of window title strings.
|
|
109
|
+
"""
|
|
110
|
+
windows = find_all_windows()
|
|
111
|
+
return sorted([w["title"] for w in windows])
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
# Initialize DPI awareness when module is imported
|
|
115
|
+
setup_dpi_awareness()
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: appsnap
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Fast Windows screenshot tool for AI coding agents
|
|
5
|
+
Project-URL: Homepage, https://github.com/detroittommy879/appsnap
|
|
6
|
+
Project-URL: Repository, https://github.com/detroittommy879/appsnap
|
|
7
|
+
Project-URL: Issues, https://github.com/detroittommy879/appsnap/issues
|
|
8
|
+
Project-URL: Documentation, https://github.com/detroittommy879/appsnap#readme
|
|
9
|
+
Project-URL: Changelog, https://github.com/detroittommy879/appsnap/releases
|
|
10
|
+
Author: appsnap contributors
|
|
11
|
+
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: ai-agent,automation,cli,desktop,screenshot,windows
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Software Development :: Testing
|
|
25
|
+
Classifier: Topic :: Utilities
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Requires-Dist: fuzzywuzzy>=0.18.0
|
|
28
|
+
Requires-Dist: pillow>=11.0.0
|
|
29
|
+
Requires-Dist: python-levenshtein>=0.25.0
|
|
30
|
+
Requires-Dist: pywin32>=306
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: black>=24.0.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
35
|
+
Requires-Dist: ruff>=0.3.0; extra == 'dev'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# 📸 appsnap
|
|
39
|
+
|
|
40
|
+
**Fast Windows screenshot tool for AI coding agents**
|
|
41
|
+
|
|
42
|
+
`appsnap` is a simple, fast CLI tool for capturing screenshots of Windows application windows. Perfect for AI agents that need to verify UI changes during development iterations.
|
|
43
|
+
|
|
44
|
+
## ✨ Features
|
|
45
|
+
|
|
46
|
+
- 🎯 **Window-specific capture** - Target apps by name with fuzzy matching
|
|
47
|
+
- � **Bulk capture** - Screenshot all windows at once for testing
|
|
48
|
+
- 🚀 **Fast** - Screenshots in ~0.1-0.3 seconds
|
|
49
|
+
- 🤖 **Agent-friendly** - JSON output and stdout paths for easy parsing
|
|
50
|
+
- 📁 **Smart defaults** - Temp directory with auto-cleanup
|
|
51
|
+
- 🔍 **List windows** - See all capturable windows
|
|
52
|
+
- 🎨 **DPI-aware** - Handles high-DPI displays correctly
|
|
53
|
+
- 🔢 **Duplicate handling** - Auto-numbers windows with identical titles
|
|
54
|
+
|
|
55
|
+
## 🚀 Quick Start
|
|
56
|
+
|
|
57
|
+
### Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Install from PyPI with uvx (recommended)
|
|
61
|
+
uvx appsnap
|
|
62
|
+
|
|
63
|
+
# Or install as a tool with uv
|
|
64
|
+
uv tool install appsnap
|
|
65
|
+
|
|
66
|
+
# Or with pipx
|
|
67
|
+
pipx install appsnap
|
|
68
|
+
|
|
69
|
+
# Install from GitHub (development version)
|
|
70
|
+
uv tool install git+https://github.com/detroittommy879/appsnap.git
|
|
71
|
+
|
|
72
|
+
# Development mode (local testing)
|
|
73
|
+
git clone https://github.com/detroittommy879/appsnap.git
|
|
74
|
+
cd appsnap
|
|
75
|
+
uv venv
|
|
76
|
+
uv pip install -e .
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Usage
|
|
80
|
+
|
|
81
|
+
**Option 1: After activating venv**
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Activate the virtual environment first
|
|
85
|
+
.venv\Scripts\activate # Windows
|
|
86
|
+
# source .venv/bin/activate # Linux/Mac
|
|
87
|
+
|
|
88
|
+
# Then use appsnap directly
|
|
89
|
+
appsnap --list
|
|
90
|
+
appsnap "Visual Studio Code"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Option 2: Using uv run (no activation needed)**
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# Run from within the appsnap directory
|
|
97
|
+
cd appsnap
|
|
98
|
+
uv run appsnap --list
|
|
99
|
+
uv run appsnap "Chrome" --output screenshot.png
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Common Commands:**
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# List all capturable windows
|
|
106
|
+
appsnap --list
|
|
107
|
+
|
|
108
|
+
# Capture a window (saves to temp directory)
|
|
109
|
+
appsnap "Visual Studio Code"
|
|
110
|
+
|
|
111
|
+
# Capture with custom output path
|
|
112
|
+
appsnap "Chrome" --output screenshot.png
|
|
113
|
+
|
|
114
|
+
# JSON output for agents
|
|
115
|
+
appsnap "Notepad" --json
|
|
116
|
+
# {"path": "C:\\Temp\\appsnap\\appsnap_20260202_153045.png", "window": "Notepad", "bbox": [100, 200, 800, 600]}
|
|
117
|
+
|
|
118
|
+
# Adjust fuzzy matching threshold (0-100, default 70)
|
|
119
|
+
appsnap "VS" --threshold 60
|
|
120
|
+
|
|
121
|
+
# Capture ALL windows to a folder (great for testing!)
|
|
122
|
+
appsnap --all ./screenshots
|
|
123
|
+
|
|
124
|
+
# Capture all with JSON summary
|
|
125
|
+
appsnap --all ./test-screens --json
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 🤖 AI Agent Integration
|
|
129
|
+
|
|
130
|
+
### Claude Desktop Skill
|
|
131
|
+
|
|
132
|
+
Add to your agent prompt or skill:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
When you need to verify UI changes, use: appsnap "App Name" --json
|
|
136
|
+
Parse the JSON output to get the screenshot path and window metadata.
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Python Integration
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
import subprocess
|
|
143
|
+
import json
|
|
144
|
+
|
|
145
|
+
result = subprocess.run(
|
|
146
|
+
["appsnap", "Chrome", "--json"],
|
|
147
|
+
capture_output=True,
|
|
148
|
+
text=True
|
|
149
|
+
)
|
|
150
|
+
data = json.loads(result.stdout)
|
|
151
|
+
screenshot_path = data["path"]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### PowerShell Integration
|
|
155
|
+
|
|
156
|
+
```powershell
|
|
157
|
+
$result = appsnap "VSCode" --json | ConvertFrom-Json
|
|
158
|
+
Write-Host "Screenshot: $($result.path)"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## 📖 CLI Options
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
positional arguments:
|
|
165
|
+
window_name Window title to search for (fuzzy matching)
|
|
166
|
+
|
|
167
|
+
options:
|
|
168
|
+
-h, --help Show this help message and exit
|
|
169
|
+
-l, --list List all capturable windows
|
|
170
|
+
-a DIR, --all DIR Capture all windows to specified directory
|
|
171
|
+
-o PATH, --output PATH
|
|
172
|
+
Output file path (default: temp directory)
|
|
173
|
+
-t N, --threshold N Fuzzy match threshold 0-100 (default: 70)
|
|
174
|
+
-j, --json Output JSON with path and metadata
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Bulk Capture Example
|
|
178
|
+
|
|
179
|
+
The `--all` flag is perfect for quickly testing that all windows capture correctly:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
# Capture all windows to a test folder
|
|
183
|
+
uv run appsnap --all ./test-captures
|
|
184
|
+
|
|
185
|
+
# Output:
|
|
186
|
+
# Capturing 42 window(s) to C:\path\to\test-captures...
|
|
187
|
+
#
|
|
188
|
+
# [OK] Visual Studio Code
|
|
189
|
+
# [OK] Chrome - Google Search
|
|
190
|
+
# [OK] Task Manager
|
|
191
|
+
# [OK] Settings
|
|
192
|
+
# [OK] PowerShell_1 # Auto-numbered duplicate
|
|
193
|
+
# [OK] PowerShell_2 # Auto-numbered duplicate
|
|
194
|
+
# ...
|
|
195
|
+
#
|
|
196
|
+
# Complete: 40 successful, 2 failed
|
|
197
|
+
# Screenshots saved to: C:\path\to\test-captures
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Windows with the same title automatically get numbered (e.g., `PowerShell_1.png`, `PowerShell_2.png`).
|
|
201
|
+
|
|
202
|
+
## 🛠️ How It Works
|
|
203
|
+
|
|
204
|
+
1. **DPI Awareness** - Sets process DPI awareness for correct scaling on high-DPI displays
|
|
205
|
+
2. **Window Enumeration** - Uses Win32 API to enumerate all visible, non-minimized windows
|
|
206
|
+
3. **Fuzzy Matching** - Finds windows using `fuzzywuzzy` for flexible name matching
|
|
207
|
+
4. **Direct Window Capture** - Uses Win32 `PrintWindow` API to capture window content directly (works even when partially occluded)
|
|
208
|
+
5. **Output** - Saves to temp or custom location, prints path to stdout for easy agent parsing
|
|
209
|
+
|
|
210
|
+
## 🔧 Development
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
# Clone and install dev dependencies
|
|
214
|
+
git clone <repo>
|
|
215
|
+
cd appsnap
|
|
216
|
+
uv pip install -e ".[dev]"
|
|
217
|
+
|
|
218
|
+
# Run tests
|
|
219
|
+
uv run pytest
|
|
220
|
+
|
|
221
|
+
# Run with local changes
|
|
222
|
+
uv run appsnap --list
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## 🐛 Troubleshooting
|
|
226
|
+
|
|
227
|
+
### "No window found matching..."
|
|
228
|
+
|
|
229
|
+
- Use `appsnap --list` to see exact window titles
|
|
230
|
+
- Try lowering the threshold: `--threshold 60`
|
|
231
|
+
- Make sure the window is visible (not minimized)
|
|
232
|
+
|
|
233
|
+
### Screenshots are blank, wrong window, or multiple windows
|
|
234
|
+
|
|
235
|
+
**v0.1.1 Fix:** Switched to Win32 PrintWindow API for direct window content capture.
|
|
236
|
+
|
|
237
|
+
This method captures the actual window content, not screen regions, so it:
|
|
238
|
+
|
|
239
|
+
- Works correctly on multi-monitor setups
|
|
240
|
+
- Captures partially occluded windows
|
|
241
|
+
- Handles DPI scaling properly
|
|
242
|
+
|
|
243
|
+
If you still have issues:
|
|
244
|
+
|
|
245
|
+
- Ensure the window is not minimized (minimized windows cannot be captured)
|
|
246
|
+
- Some apps (especially GPU-accelerated ones) may not respond to PrintWindow correctly
|
|
247
|
+
- Try bringing the window to foreground if capture fails
|
|
248
|
+
|
|
249
|
+
### DPI/Scaling issues
|
|
250
|
+
|
|
251
|
+
The tool automatically handles DPI awareness. If you see incorrect sizing:
|
|
252
|
+
|
|
253
|
+
- Ensure Windows display scaling is consistent
|
|
254
|
+
- Check that the app respects DPI settings
|
|
255
|
+
|
|
256
|
+
## 📝 License
|
|
257
|
+
|
|
258
|
+
MIT License - see [LICENSE](LICENSE) for details
|
|
259
|
+
|
|
260
|
+
## 🙏 Acknowledgments
|
|
261
|
+
|
|
262
|
+
Built on the excellent Windows-MCP project patterns and libraries:
|
|
263
|
+
|
|
264
|
+
- [Pillow](https://github.com/python-pillow/Pillow) - Screenshot capture and image processing
|
|
265
|
+
- [fuzzywuzzy](https://github.com/seatgeek/fuzzywuzzy) - Fuzzy string matching
|
|
266
|
+
- [pywin32](https://github.com/mhammond/pywin32) - Windows API access
|
|
267
|
+
|
|
268
|
+
## 🤝 Contributing
|
|
269
|
+
|
|
270
|
+
Contributions welcome! Please feel free to submit issues or pull requests.
|
|
271
|
+
|
|
272
|
+
For maintainers publishing to PyPI, see [PUBLISHING.md](PUBLISHING.md) for detailed instructions on:
|
|
273
|
+
|
|
274
|
+
- Setting up GitHub Actions for automated PyPI releases
|
|
275
|
+
- Manual publishing workflow
|
|
276
|
+
- AI agent skill integration
|
|
277
|
+
- Troubleshooting
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
Made for AI agents, by developers 🤖❤️
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
appsnap/__init__.py,sha256=1Ol3w5E4NGkkHDEAW6LuNmn88V-096KeP-q29_wviWg,402
|
|
2
|
+
appsnap/__main__.py,sha256=AkKtGArHydEHwueO414hEPekM03rw221NIMaKCEhFCU,8348
|
|
3
|
+
appsnap/capture.py,sha256=PB8Y2Q5-h6BkR7fVt8Yqf1QNv63OrR1Z_zL7IGVOqHo,4555
|
|
4
|
+
appsnap/windows.py,sha256=cCvz--9N-kTSC8axxegV8Ne9qIwPkqIwyh3DNmyyYEQ,3537
|
|
5
|
+
appsnap-0.2.0.dist-info/METADATA,sha256=UeyR5zSbD8_hCzaz-eikkonQOJkjRdgm-UTpBctHpZ4,8132
|
|
6
|
+
appsnap-0.2.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
7
|
+
appsnap-0.2.0.dist-info/entry_points.txt,sha256=l1gGnZkAyFUqrlPGaCnU5vpl-n7KNcie_Nq3q3XgN2A,50
|
|
8
|
+
appsnap-0.2.0.dist-info/licenses/LICENSE,sha256=n43RGUuODSNb4emThiS6Zt7F3234aAXVRthApBekBio,1098
|
|
9
|
+
appsnap-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 appsnap contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|