app.ocr - 文字识别 API

基于 Vision 框架的光学字符识别(OCR)。

需要 macOS 10.15+

方法

app.ocr.recognize(imagePath, opts?)

识别图片中的文字。

参数:

  • imagePath (string) - 图片文件路径
  • opts (table, 可选):
    • language (string|array) - 识别语言(如 "zh-Hans", {"zh-Hans", "en"}
    • level (string) - 识别级别: "accurate"(默认) 或 "fast"
    • rect (table) - 识别区域(像素坐标): {x, y, w, h}

返回值: array<table>, error - 每项包含:

  • text (string) - 识别的文字
  • confidence (number) - 置信度(0.0-1.0)
-- 识别整张图片
local results, err = app.ocr.recognize("/path/to/image.png")
if results then
    for _, item in ipairs(results) do
        app.log.info(item.text .. " (" .. string.format("%.0f%%", item.confidence * 100) .. ")")
    end
end

-- 指定语言和区域
local results = app.ocr.recognize("/path/to/image.png", {
    language = {"zh-Hans", "en"},
    level = "accurate",
    rect = {x = 100, y = 50, w = 500, h = 200}
})

app.ocr.recognizeFromScreen(opts?)

截取屏幕并识别文字。

参数:

  • opts (table, 可选):
    • displayId (number) - 显示器 ID
    • rect (table) - 截取区域: {x, y, w, h}
    • language (string|array) - 识别语言
    • level (string) - 识别级别

返回值: array<table>, error - 同 recognize()

-- 截取整个屏幕并识别
local results, err = app.ocr.recognizeFromScreen()

-- 截取指定区域
local results = app.ocr.recognizeFromScreen({
    rect = {x = 0, y = 0, w = 800, h = 600},
    language = "en"
})

app.ocr.languages()

获取系统支持的 OCR 语言列表。

返回值: array<string>, error

local langs = app.ocr.languages()
-- {"en-US", "fr-FR", "it-IT", "de-DE", "es-ES", "pt-BR", "zh-Hans", "zh-Hant", ...}

示例

提取图片中的文字

function MyPlugin:handleOCR(context)
    local file = context.selectedFiles[1]

    app.progress.show("识别中", {message = "正在识别文字..."})
    local results, err = app.ocr.recognize(file, {
        language = {"zh-Hans", "en"},
        level = "accurate"
    })
    app.progress.hide()

    if not results then
        app.dialog.alert("错误", err or "识别失败")
        return
    end

    if #results == 0 then
        app.dialog.alert("结果", "未识别到文字")
        return
    end

    local text = ""
    for _, item in ipairs(results) do
        text = text .. item.text .. "\n"
    end

    app.clipboard.setText(text)
    app.notification.show("完成", "识别到 " .. #results .. " 段文字,已复制到剪贴板")
end
开发者文档
使用帮助
使用说明 脚本菜单 常见问题
脚本开发
开发指南
插件开发
快速开始 开发指南 示例插件
API 参考
概览 API 查询 插件信息 日志 Finder 上下文 插件设置 国际化
UI 与交互
对话框 进度条 系统通知 选择器 WebView 状态栏 Dock
文件与路径
文件操作 路径工具 Finder 操作 废纸篓 扩展属性 元数据 文件监听
数据格式
JSON Plist CSV XML PDF 图片
文本与编码
字符串 正则表达式 日期时间 颜色 加密编码
系统
Shell 命令 进程管理 应用管理 系统信息 AppleScript 快捷指令
系统信息
网络信息 电源/电池 屏幕/外观 音频控制 蓝牙设备 位置服务
网络
HTTP 请求 WebSocket URL 工具
输入与剪贴板
键盘模拟 鼠标模拟 全局热键 剪贴板 窗口管理
存储
SQLite Keychain UserDefaults
媒体
文字识别 二维码
工具
归档 类型标识 分享 定时器 防休眠 并发/协程