Windows 桌面自动化
工具概览
| 脚本 | 功能 |
|---|---|
scripts/mouse.py |
鼠标:移动、点击、双击、拖拽、滚动、截图 |
scripts/keyboard.py |
键盘:文字输入、单键、组合热键 |
scripts/window.py |
窗口:列举、查找、聚焦、最大化/最小化、关闭、移动 |
scripts/screenshot_powershell.py |
🆕 PowerShell 截图:零依赖,支持全屏/区域/多显示器截图 |
scripts/screenshot_and_send.py |
截取全屏并准备发送到群聊 |
scripts/send_file.py |
发送任意文件(Word/PDF/图片等)到群聊 |
截图功能对比
| 方式 | 命令 | 依赖 | 优点 | 适用场景 |
|---|---|---|---|---|
| Python 截图 | python mouse.py screenshot |
pyautogui + pillow | 与鼠标操作集成,流程统一 | 自动化流程中,已安装依赖 |
| PowerShell 截图 | python screenshot_powershell.py |
无(Windows 原生) | 零依赖,支持区域/多显示器 | 轻量环境,不想装依赖 |
| 截图+发送 | python screenshot_and_send.py |
pyautogui + pillow | 截图后自动准备发送 | 需要截图后推送到群聊 |
快速上手
安装依赖
# 完整安装(使用 Python 截图和鼠标控制功能时需要)
pip install pyautogui pillow pywin32
# 如果只使用 PowerShell 截图 + 键盘 + 窗口管理,则只需:
pip install pywin32
鼠标操作
python scripts/mouse.py pos # 查看当前坐标
python scripts/mouse.py move 500 300 # 移动鼠标
python scripts/mouse.py click 500 300 # 左键点击
python scripts/mouse.py click 500 300 right # 右键点击
python scripts/mouse.py dblclick 500 300 # 双击
python scripts/mouse.py drag 100 100 500 300 # 拖拽
python scripts/mouse.py scroll 500 300 3 # 向上滚动3格
python scripts/mouse.py screenshot output.png # 截图(需要 pyautogui)
键盘操作
python scripts/keyboard.py type "你好世界" # 输入文字(支持中文)
python scripts/keyboard.py press enter # 按回车
python scripts/keyboard.py press tab # 按Tab
python scripts/keyboard.py hotkey ctrl c # Ctrl+C
python scripts/keyboard.py hotkey ctrl alt delete # Ctrl+Alt+Del
python scripts/keyboard.py hotkey win r # Win+R 打开运行
窗口管理
python scripts/window.py list # 列出所有窗口
python scripts/window.py find "记事本" # 查找窗口
python scripts/window.py focus "记事本" # 激活窗口
python scripts/window.py maximize "记事本" # 最大化
python scripts/window.py minimize "记事本" # 最小化
python scripts/window.py close "记事本" # 关闭
PowerShell 截图(零依赖)🆕
# 全屏截图(主显示器)
python scripts/screenshot_powershell.py # 默认 screenshot.png
python scripts/screenshot_powershell.py desktop.png # 指定文件名
# 区域截图
python scripts/screenshot_powershell.py --region 0,0,800,600 # 截左上角 800×600
python scripts/screenshot_powershell.py shot.png --region 100,200,1024,768
# 多显示器全屏截图
python scripts/screenshot_powershell.py --all # 所有显示器拼接
python scripts/screenshot_powershell.py all_screens.png --all # 指定文件名
底层原理:通过 PowerShell 调用 .NET 的 System.Windows.Forms 和 System.Drawing 库直接截取桌面像素,无需安装任何 Python 第三方库。
截图并发送文件
# 1. 截图到 workspace
python scripts/screenshot_and_send.py # 默认命名
python scripts/screenshot_and_send.py "会议记录" # 自定义名称前缀
# 2. 发送任意文件到群聊
python scripts/send_file.py "文档.docx" # 发送 Word
python scripts/send_file.py "报告.pdf" # 发送 PDF
python scripts/send_file.py "截图.png" # 发送图片
执行方式
直接通过 exec 工具在工作区运行脚本:
exec("python C:\\Users\\Administrator\\.openclaw\\workspace\\skills\\windows-automation\\scripts\\mouse.py click 500 300")
或切换到技能目录后运行:
exec("python scripts/mouse.py click 500 300", cwd="C:\\Users\\Administrator\\.openclaw\\workspace\\skills\\windows-automation")
参考资料
详见 references/reference.md,包含:坐标系统说明、紧急停止方法、常用热键速查、典型流程示例。
注意事项
- ⚠️ 紧急中止:将鼠标快速移到屏幕左上角 (0,0) 可中止所有自动化操作
- 中文输入通过剪贴板实现,需确保目标窗口已聚焦
- 部分操作(如 UAC 弹窗)需要管理员权限