mcpField
U
ubuntu-desktop-control

ubuntu-desktop-control

github.com/lommaj生产力1169 次安装37 周前更新
代码仓库

关于 ubuntu-desktop-control

ubuntu-desktop-control 是通过界面元素名称控制 Linux 桌面的技能,不必只依赖坐标。它结合辅助功能树定位与 OCR 备用方案,并提供等待和点击验证。

主要功能

  1. 01按名称和角色查找元素
  2. 02AT-SPI 定位及 OCR 备用方案
  3. 03等待元素出现或消失
  4. 04使用 OCR 验证点击
  5. 05基于坐标的桌面命令

适用场景

  1. 01点击桌面应用中的带标签按钮
  2. 02辅助功能数据不可用时查找屏幕文字
  3. 03等待交易或加载提示出现
  4. 04自动化点击无标签图标的界面步骤

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/lommaj/ubuntu-desktop-control --skill ubuntu-desktop-control

安装后在 Claude Code 中输入/ubuntu-desktop-control确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

AGENTS.md
CLAUDE.md
LICENSE
install.sh
README.md
requirements.txt
scripts/
desktop.py
src/desktop_control/
__init__.py
annotate.py
atspi.py
core.py
cache.py
finder.py
screenshot.py
element.py
ocr.py
waiter.py
xdotool.py
tests/
test_regressions.py

详细介绍

桌面控制技能

使用语义化元素定位来控制桌面 GUI。按名称查找并点击 UI 元素,而不是使用坐标。

主要功能:

  • AT-SPI - 使用无障碍树的主要方法(可识别元素角色、状态和操作)
  • OCR 回退 - 当 AT-SPI 找不到元素时,使用基于 Tesseract 的文本查找
  • 等待实用工具 - 使用指数退避轮询,等待元素出现
  • 点击验证 - 可选的点击前截图验证

先决条件

安装依赖项:

bash install.sh

或手动安装:

# System packages
sudo apt-get install -y xdotool scrot imagemagick \
    at-spi2-core libatk-adaptor python3-gi gir1.2-atspi-2.0 \
    tesseract-ocr tesseract-ocr-eng python3-pip

# Python packages
pip3 install -r requirements.txt

对于无头 Xvfb 会话:

export GTK_MODULES=gail:atk-bridge
export QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
/usr/lib/at-spi2-core/at-spi-bus-launcher &

命令

所有命令默认使用 DISPLAY=:10.0。使用 --display 标志覆盖。


find-element

通过 AT-SPI 查找 UI 元素,失败时回退到 OCR。

python3 scripts/desktop.py find-element --name "Confirm" [--role button] [--app Firefox]
参数类型必填描述
--name、-nstring否要查找的元素名称/文本
--role、-rstring否元素角色(button、entry 等)
--app、-astring否按应用名称筛选
--allflag否查找所有匹配项
--clickableflag否仅查找可点击元素
--max-resultsint否最大结果数(默认:50)

返回:

{
  "element": {
    "name": "Confirm",
    "bounds": { "x": 400, "y": 300, "width": 100, "height": 30 },
    "center": { "x": 450, "y": 315 },
    "role": "push button",
    "source": "atspi",
    "visible": true,
    "enabled": true,
    "clickable": true
  }
}

find-text

仅通过 OCR 查找屏幕上的文本。

python3 scripts/desktop.py find-text "I have an existing wallet" [--exact] [--all]
参数类型必填描述
textstring是要查找的文本
--exactflag否要求完全匹配
--case-sensitiveflag否区分大小写匹配
--allflag否查找所有匹配项
--max-resultsint否最大结果数(默认:50)

返回:

{
  "match": {
    "name": "I have an existing wallet",
    "bounds": { "x": 200, "y": 400, "width": 180, "height": 20 },
    "center": { "x": 290, "y": 410 },
    "source": "ocr",
    "confidence": 95.2
  }
}

click-element

按名称/角色点击元素(先查找元素,再点击其中心位置)。

python3 scripts/desktop.py click-element --name "Next" [--role button] [--verify]
参数类型必填描述
--name、-nstring否元素名称/文本
--role、-rstring否元素角色
--app、-astring否按应用名称筛选
--rightflag否右键点击
--doubleflag否双击
--verifyflag否点击前通过 OCR 验证

click-element 至少需要一个选择器:--name 或 --role。 使用 --verify 时,必须能够使用 OCR,并且必须提供文本(通常通过 --name 提供)。

返回:

{
  "clicked": {
    "element": { "name": "Next", "..." },
    "x": 450,
    "y": 315,
    "button": "left",
    "double": false
  }
}

wait-for

等待元素或文本出现(带超时和指数退避)。

python3 scripts/desktop.py wait-for --name "Success" --timeout 30
python3 scripts/desktop.py wait-for --text "Transaction complete" --timeout 60
python3 scripts/desktop.py wait-for --name "Loading" --gone --timeout 30
参数类型必填描述
--name、-nstring否元素名称(AT-SPI + OCR)
--role、-rstring否元素角色(仅 AT-SPI)
--app、-astring否应用筛选条件
--text、-tstring否要查找的文本(仅 OCR)
--exactflag否文本完全匹配
--goneflag否等待元素消失
--timeoutfloat否超时时间,单位为秒(默认:30)

单次调用请使用 --text 或元素选择器(--name、--role、--app)之一,不要同时使用两者。 等待元素时(无论是否使用 --gone),至少提供 --name 或 --role 之一。

返回:

{ "found": { "name": "Success", "..." } }
// or
{ "gone": true, "name": "Loading" }
// or
{ "error": "Element not found within 30s", "timeout": true }

list-elements

列出所有交互式元素(按钮、输入框、链接等)。

python3 scripts/desktop.py list-elements [--app Firefox] [--role button]
参数类型必填描述
--app、-astring否按应用名称筛选
--role、-rstring否按角色筛选
--include-hiddenflag否包含隐藏元素
--max-resultsint否最大结果数(默认:100)

返回:

{
  "elements": [
    { "name": "Sign In", "role": "push button", "..." },
    { "name": "Email", "role": "entry", "..." }
  ],
  "count": 2
}

status

检查 AT-SPI 和 OCR 是否可用。

python3 scripts/desktop.py status

返回:

{
  "atspi": {
    "available": true,
    "applications": ["Firefox", "gnome-calculator"]
  },
  "ocr": { "available": true },
  "display": ":10.0"
}

原始命令

这些基于坐标的命令仍然可用:

命令描述
screenshot [--output PATH]截取屏幕截图
click X Y [--right] [--double]点击指定坐标
type "TEXT" [--type-delay MS]输入文本
key "KEYS"按下组合键
move X Y移动鼠标
active获取活动窗口信息
find-window "NAME"按名称查找窗口
focus "NAME"按名称聚焦窗口
position获取鼠标位置
windows列出所有窗口

工作流示例

MetaMask 交易(语义操作)
# Wait for and click Confirm button
python3 scripts/desktop.py wait-for --name "Confirm" --role button --timeout 30
python3 scripts/desktop.py click-element --name "Confirm" --role button

# Wait for success message
python3 scripts/desktop.py wait-for --text "Transaction submitted" --timeout 60
Phantom 钱包导入(语义操作)
# Click "I have an existing wallet"
python3 scripts/desktop.py click-element --name "I already have a wallet"

# Wait for seed phrase input
python3 scripts/desktop.py wait-for --role entry --timeout 10

# Type seed phrase
python3 scripts/desktop.py type "word1 word2 word3..."

# Click Import
python3 scripts/desktop.py click-element --name "Import"
混合方式(语义操作 + 坐标)
# Use semantic for known buttons
python3 scripts/desktop.py click-element --name "Settings"

# Fall back to coordinates for unlabeled icons
python3 scripts/desktop.py screenshot --output /tmp/screen.png
# (analyze screenshot to get coordinates)
python3 scripts/desktop.py click 850 120

提示

  1. 优先使用语义命令——click-element 和 wait-for 比基于坐标的命令更可靠
  2. 先检查状态——运行 status,确认 AT-SPI 和 OCR 可用
  3. 使用 --role 提高精确度——区分名称相同的按钮和文本
  4. 回退到 OCR——如果 AT-SPI 未提供某个元素,find-text 会使用 OCR
  5. 等待而不是休眠——wait-for 比固定延迟更可靠
  6. 对关键点击使用 --verify——点击前添加 OCR 验证

评论

支持 Markdown