app.uti - 统一类型标识符 API

macOS 统一类型标识符(UTI)查询与转换。

权限级别: L0(安全,始终可用)

方法

app.uti.fromExtension(ext)

将文件扩展名转换为 UTI。

参数:

  • ext (string) - 文件扩展名(如 “pdf”)

返回值: string, error - UTI 字符串

local uti = app.uti.fromExtension("pdf")    -- "com.adobe.pdf"
local uti = app.uti.fromExtension("png")    -- "public.png"
local uti = app.uti.fromExtension("swift")  -- "public.swift-source"

app.uti.fromMimeType(mime)

将 MIME 类型转换为 UTI。

参数:

  • mime (string) - MIME 类型

返回值: string, error

local uti = app.uti.fromMimeType("application/pdf")  -- "com.adobe.pdf"
local uti = app.uti.fromMimeType("image/png")        -- "public.png"

app.uti.toExtension(uti)

将 UTI 转换为首选文件扩展名。

参数:

  • uti (string) - UTI 字符串

返回值: string, error

local ext = app.uti.toExtension("com.adobe.pdf")  -- "pdf"
local ext = app.uti.toExtension("public.png")     -- "png"

app.uti.toMimeType(uti)

将 UTI 转换为 MIME 类型。

参数:

  • uti (string) - UTI 字符串

返回值: string, error

local mime = app.uti.toMimeType("com.adobe.pdf")  -- "application/pdf"

app.uti.conforms(uti, parent)

检查 UTI 是否符合父类型。

参数:

  • uti (string) - UTI 字符串
  • parent (string) - 父 UTI 字符串

返回值: boolean

app.uti.conforms("public.png", "public.image")     -- true
app.uti.conforms("com.adobe.pdf", "public.data")   -- true
app.uti.conforms("public.png", "public.movie")     -- false

app.uti.forFile(path)

获取文件的 UTI。

参数:

  • path (string) - 文件路径

返回值: string, error

local uti = app.uti.forFile("/path/to/image.png")  -- "public.png"

app.uti.description(uti)

获取 UTI 的本地化描述。

参数:

  • uti (string) - UTI 字符串

返回值: string|nil

local desc = app.uti.description("com.adobe.pdf")  -- "PDF 文稿"
local desc = app.uti.description("public.png")     -- "PNG 图像"

示例

按文件类型分类

function MyPlugin:handleClassify(context)
    local categories = {}
    for _, file in ipairs(context.selectedFiles) do
        local uti = app.uti.forFile(file)
        if uti then
            local category = "other"
            if app.uti.conforms(uti, "public.image") then
                category = "images"
            elseif app.uti.conforms(uti, "public.movie") then
                category = "videos"
            elseif app.uti.conforms(uti, "public.audio") then
                category = "audio"
            elseif app.uti.conforms(uti, "public.text") then
                category = "text"
            end

            categories[category] = (categories[category] or 0) + 1
        end
    end

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