Files
37duAutomation/.codebuddy/skills/37du-Desktop-Auto/SKILL.md
T

132 lines
8.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: 37du-Desktop-Auto
description: >
37duAutomation 桌面自动化库 desktopauto(仓库 libs/desktopauto)使用总纲:获取窗口对象、
激活/关闭窗口、识图点击、图像存在判断、等待图像、模板匹配(多尺度/多目标)、OCR 通用文字识别
与文字坐标、键盘输入(中文走剪贴板粘贴)、组合键、滚轮拖拽、找色、界面差异比对、
启动或打开程序、终止进程、离线标记调试。
当任务涉及「目标程序拿不到元素树(无 DOM / 无 UIA)只能用屏幕坐标操作」、需要识图/找图/
点击图像、按文字定位点击、在 Windows 桌面上自动化某个客户端(钉钉、WPS、Qt 程序、老系统)时,
加载本技能掌握正确调用顺序与坑位,再动手写代码。
触发关键词:桌面自动化、识图、找图、模板匹配、点击图像、图像存在、等待图像、鼠标悬停、
OCR、文字识别、文字坐标、找色、界面差异、窗口对象、获取窗口、激活窗口、关闭窗口、
启动程序、终止程序、键鼠操作、desktopauto、RPA、拿不到元素树。
---
# 桌面自动化库 desktopauto 使用总纲
> 适用对象:**没有元素树**的桌面程序(钉钉客户端、Qt/Electron 自定义渲染、远程桌面里的老系统)。
> 有接口走接口、有 DOM/UIA 走元素定位,两者都拿不到才用本库。
## 0. 位置、依赖、自测
| 项 | 值 |
|---|---|
| 库源码 | `libs/desktopauto/`(包名 `desktopauto`) |
| 依赖 | `opencv-python` `numpy` `pillow`;OCR 另需 `rapidocr` `onnxruntime`(模型随包、离线) |
| 安装(开发机) | `.venv\Scripts\python.exe -m pip install -e libs\desktopauto --no-deps` |
| 未安装时 | `sys.path.insert(0, r"d:\work\code\37duAutomation\libs\desktopauto")` 也能直接 import |
| 自测 | `.venv\Scripts\python.exe libs\desktopauto\_selftest.py`(60 项,含真实窗口/进程查询) |
| 完整 API | `references/api-reference.md`(按模块列函数签名,改动后同步) |
## 1. 通道决策顺序(先想清楚,别一上来就 OCR)
1. **有 HTTP 接口** → 走接口(如 HR/OA 的业务接口),最快最稳。
2. **有 DOM/UIA** → 走元素定位(浏览器 DrissionPage、Win32 控件的 `uiautomation`)。
3. **都没有** → 本库,按元素特征选手段:
- 按钮/图标/固定图形 → **模板匹配**(`find` / `click`);
- 只有文字(列表项、菜单项、动态文案)→ **OCR**(`find_text` / `click_text`);
- 只关心有无/颜色/变化 → **找色 / 界面差异**(`find_color` / `diff`);
- 组合兜底:`find(模板)` 失败再 `find_text(文字)`,用 `wait_any` 一次等两种。
## 2. 标准作业流程(照这个顺序写)
```python
from desktopauto import get_window, paste, hotkey, wait_any
w = get_window(process="DingTalk.exe", timeout=20) # ① 拿窗口对象(找不到直接抛错)
w.activate() # ② 激活到前台(点击前必须)
s = w.screen(threshold=0.9, debug_dir="debug") # ③ 绑定 Screen:后续截图/匹配都限定在窗口内
s.click("img/chat_input.png") # ④ 识图点击(找到才点,返回 bool)
paste("排班识别,水庄大鹅……") # ⑤ 中文输入走剪贴板
hotkey("ctrl", "enter") # ⑥ 组合键
r = wait_any(texts=["发送成功", "发送失败"], timeout=20) # ⑦ 等校验锚点
print(r["kind"], r["target"], r["match"].center)
```
关键点:
- **每次操作前重新定位**,绝不缓存坐标跨步骤使用(窗口一移动全废);
- `Screen(window=...)` / `w.screen()` 限定范围,既快又不会误命中全屏同款元素;
- 关键步骤都要有**可断言的锚点**(`wait_text` / `wait_image` / `diff`),否则失败无法定位。
## 3. 常用调用速查
| 需求 | 调用 |
|---|---|
| 图像是否存在 | `image_exists(tpl)` / `s.exists(tpl)` |
| 等图像出现 / 消失 | `wait_image(tpl, timeout=10)` / `wait_gone(tpl, timeout=10)` |
| 等任一(图或文字) | `wait_any(templates=[a, b], texts=["成功"], timeout=30)` |
| 点击图像 / 悬停图像 | `click_image(tpl)` / `hover_image(tpl)` |
| 多状态按钮(可用/禁用/角标) | `find_any([tpl1, tpl2])` → `(Match, 模板名)` |
| 文字定位(含坐标) | `read_text()` → `[TextItem(text, center, score)]` |
| 只要文字(喂 LLM) | `ocr_text()` |
| 按文字判断/点击/悬停 | `text_exists("导出")` / `click_text("导出")` / `hover_text("导出")` |
| 输入中文 | `paste(text)`(写剪贴板 + Ctrl+V) |
| 按键 / 组合键 | `press("enter")` / `hotkey("ctrl", "s")` |
| 按键串(影刀写法) | `send_keys("^a{ENTER}")` / `send_keys("%{F4}")` |
| 移动鼠标 | `move_mouse(x, y, relative_to="window", speed="instant")` |
| 等待(可随机) | `sleep(1.5)` / `sleep(random_range=(1, 3))` |
| 文件 / 文件夹 | `fs.exists / write_text / read_text / copy / move / list_files / newest_file / wait_file` |
| 等所有图出现 | `wait_all([a, b], timeout=20)` |
| 滚轮 / 拖拽 | `scroll(-5, at=tpl)` / `drag(tpl, (x, y))` |
| 找色(状态点/高亮行) | `find_color("#FF3B30", tolerance=15, min_area=8)` |
| 界面是否变化 | `diff("before.png")["changed_ratio"]` |
| 启动 / 打开 | `run_or_open(r"...\DingTalk.exe")`、`open_path(xlsx)`、`open_url(url)` |
| 等待进程 | `wait_process("DingTalk", timeout=30)`、`until="exit"` 等退出 |
| 关闭 / 终止 | `w.close()` + `w.wait_closed()`;`kill_process("DingTalk")` 强杀 |
| 离线验证(不动鼠标) | `mark(image, [tpl1, tpl2], out_path)` → 标注图 |
| 命令行验证 | `python -m desktopauto --mode ocr -i 1.png --text "导出"` 等 |
## 4. 模板图怎么准备(决定成败)
1. 先截一张:`python -m desktopauto --mode shot --path shot.png --window "钉钉"`;
2. 裁**目标区域本身**(带完整边框/图标),不要带会变的内容(时间、数字、用户名、红点);
3. 模板必须有可辨识特征:纯色/近似纯色会被自动降级为像素匹配,容易在别处命中;
4. 跨缩放机器复用同一模板时传 `scales=(0.9, 1.0, 1.1)`,或按机器各截一份;
5. 命中不稳定先看 `debug_dir` 里的 `hit_*.png` / `miss_*.png`(带框和分数),别盲调阈值。
## 5. 坑位清单(踩过才知道的)
| 现象 | 处理 |
|---|---|
| 图在屏幕上但点偏 | 进程 DPI 未感知;库首次调用已 `SetProcessDpiAwareness(2)`,混用别的库时需重启进程 |
| 截出来全黑 | 锁屏 / 远程桌面断开 / 非交互会话;执行机必须保持已登录且解锁 |
| 窗口被挡还能找到吗 | 不能——截的是屏幕实际内容;点击前先 `w.activate()` |
| 输入没反应 | 目标以管理员运行、本进程不是(UIPI 拦截);或焦点不在目标上 |
| 粘贴出旧内容 | `paste(delay=0.15)` 加延迟,或 `restore=False` |
| 后台/最小化窗口 | 只能截图不能点击;`find_windows` 默认排除最小化窗口 |
| OCR 太慢 | 全屏 2~4s;**必须限定 `window`/`region`**,且一屏多词时先 `read_text()` 再本地过滤 |
| 多显示器 | 坐标是虚拟屏幕坐标(可能为负),全屏截图包含所有屏 |
## 6. 与插件/脚本集成
- **插件里用**:执行机环境包预装(`python-env\<版本>\python.exe -m pip install <repo>\libs\desktopauto`),
插件 `requirements.txt` 声明 `desktopauto`;如需插件包完全自包含,把 `desktopauto/` 目录复制进插件包内相对导入。
- **影刀工作流里用**:`sys.path.insert` 挂到 `libs\desktopauto` 后直接 import(不要复制进单文件)。
- 改动库后**必须**跑 `_selftest.py`,并把 `references/api-reference.md` 同步更新。
### 从影刀工程迁移
拿到影刀工程导出目录(含 `imagesV2.xml` + `resources\images\*.png` + `process*.py`):
1. 跑 `libs/desktopauto/scripts/import_yingdao_images.py`(改脚本顶部 `SRC_PROJECT` / `OUTPUT_ROOT` 两个常量),
把模板按中文名导出到 `%LOCALAPPDATA%\37duAutomation\desktopauto_templates\<工程名>\`,
同时生成 `manifest.json`(记录 `threshold`(similarity/100)、`gray`(graying)、`scales`(adaptive 时 0.9/1.0/1.1))。
2. 阅读 `process*.py` 就能拿到真实用到的指令与参数,按 `README.md` 的「影刀 → 本库」对照表改写:
`image.click` → `click(tpl, timeout=, delay_after=, anchor=)`、`image.wait` → `wait/wait_gone/wait_all`、
`win32.send_keys` → `send_keys`、`win32.move_mouse` → `move_mouse`、`programing.sleep` → `sleep`、
`file.if_exist`/`dir.makedir` → `fs.exists`/`fs.make_dir`。
3. `process.run` / `process.invoke_module`(调流程或工程内 Python 模块)不迁,直接改成普通函数调用。