take_screenshot
Capture and save screen images on Windows systems. Specify file path and format (PNG or JPG) for automated screenshot management.
Instructions
截取屏幕截图
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | 保存路径(可选,默认桌面) | |
| format | No | 图片格式(可选,默认 png) |
Implementation Reference
- src/tools/screen.js:59-95 (handler)The handler function that executes the screenshot capture using PowerShell script on Windows, saving to desktop or specified path with optional format.async takeScreenshot(savePath, format = 'png') { try { // 生成文件名 const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const fileName = `screenshot-${timestamp}.${format}`; const defaultPath = path.join(process.env.USERPROFILE, 'Desktop', fileName); const finalPath = savePath || defaultPath; // 使用 PowerShell 截图 const script = ` Add-Type -AssemblyName System.Windows.Forms,System.Drawing $bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds $bitmap = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height $graphics = [System.Drawing.Graphics]::FromImage($bitmap) $graphics.CopyFromScreen($bounds.Location, [System.Drawing.Point]::Empty, $bounds.Size) $bitmap.Save("${finalPath}", [System.Drawing.Imaging.ImageFormat]::${format === 'jpg' ? 'Jpeg' : 'Png'}) $bitmap.Dispose() $graphics.Dispose() `; await execAsync(`powershell -Command "${script.replace(/"/g, '\\"')}"`, { shell: 'powershell.exe' }); // 验证文件是否创建 await fs.access(finalPath); return { success: true, path: finalPath, format, message: '截图已保存' }; } catch (error) { return { success: false, error: error.message }; } }
- src/tools/screen.js:14-24 (schema)The tool definition including name, description, and input schema for validation.{ name: 'take_screenshot', description: '截取屏幕截图', inputSchema: { type: 'object', properties: { path: { type: 'string', description: '保存路径(可选,默认桌面)' }, format: { type: 'string', enum: ['png', 'jpg'], description: '图片格式(可选,默认 png)' }, }, }, },
- src/tools/screen.js:48-57 (registration)The executeTool method registers and dispatches 'take_screenshot' to its handler.async executeTool(name, args) { switch (name) { case 'take_screenshot': return await this.takeScreenshot(args.path, args.format); case 'take_screenshot_region': return await this.takeScreenshotRegion(args.x, args.y, args.width, args.height, args.path); default: throw new Error(`未知工具: ${name}`); } }
- src/tools/screen.js:43-46 (registration)Checks if 'take_screenshot' is a handled tool.canHandle(toolName) { const tools = ['take_screenshot', 'take_screenshot_region']; return tools.includes(toolName); }