app.chooser - 快速选择器 API

提供类似 Raycast/Alfred 的快速选择界面,支持搜索过滤。

权限: 需在 manifest 中声明 chooser(L1 standard)

方法

app.chooser.show(items, opts?)

显示快速选择器窗口。

参数:

  • items (array) - 选项列表,每项为 table:
    • text (string) - 主文本(必填)
    • subText (string, 可选) - 副文本/描述
    • icon (string, 可选) - 图标(支持 sf:symbol.name 格式使用 SF Symbols)
    • id (string|number, 可选) - 唯一标识
  • opts (table, 可选):
    • title (string, 可选) - 窗口标题
    • placeholder (string, 可选) - 搜索框占位符
    • multiSelect (boolean, 可选) - 是否多选(默认 false)
    • width (number, 可选) - 窗口宽度(默认 500)
    • maxVisibleItems (number, 可选) - 最大可见项数(默认 10)

返回值:

  • 单选:选中的 item table 或 nil(取消)
  • 多选:选中的 item 数组或 nil(取消)
-- 基本使用
local selected = app.chooser.show({
    {text = "新建文件",     subText = "创建空白文件",   icon = "sf:doc.badge.plus"},
    {text = "新建文件夹",   subText = "创建空文件夹",   icon = "sf:folder.badge.plus"},
    {text = "从模板创建",   subText = "使用预定义模板", icon = "sf:doc.on.doc"},
}, {
    title = "快速操作",
    placeholder = "搜索操作..."
})

if selected then
    app.log.info("选择了: " .. selected.text)
end

dialog.choose 的区别

特性 app.chooser.show app.dialog.choose
UI 样式 独立浮动窗口 + 搜索框 NSAlert 下拉/复选列表
搜索过滤 支持实时搜索 不支持
副文本 支持 subText 不支持
图标 支持 SF Symbols 不支持
适用场景 大量选项快速筛选 少量选项简单选择

示例

快速打开项目

function MyPlugin:handleQuickOpen(context)
    -- 构建项目列表
    local items = {}
    local projects = app.file.find("~/Projects", {type = "directory", maxDepth = 1})

    for _, path in ipairs(projects) do
        table.insert(items, {
            id = path,
            text = app.path.basename(path),
            subText = path,
            icon = "sf:folder.fill"
        })
    end

    local selected = app.chooser.show(items, {
        title = "打开项目",
        placeholder = "搜索项目..."
    })

    if selected then
        app.shell.execute("open " .. app.shell.quote(selected.id))
    end
end

命令面板

function MyPlugin:handleCommandPalette(context)
    local commands = {
        {id = "copy_path",   text = "复制路径",     subText = "复制文件完整路径", icon = "sf:doc.on.clipboard"},
        {id = "copy_name",   text = "复制文件名",   subText = "复制文件名称",     icon = "sf:textformat"},
        {id = "open_term",   text = "在终端打开",   subText = "打开 Terminal",    icon = "sf:terminal"},
        {id = "compress",    text = "压缩文件",     subText = "创建 ZIP 压缩包",  icon = "sf:archivebox"},
        {id = "hash",        text = "计算哈希",     subText = "SHA256 校验",      icon = "sf:number"},
    }

    local selected = app.chooser.show(commands, {
        placeholder = "输入命令..."
    })

    if selected then
        self:executeCommand(selected.id, context)
    end
end

多选操作

function MyPlugin:handleSelectTags(context)
    local tags = {
        {id = "important", text = "重要",   icon = "sf:star.fill"},
        {id = "urgent",    text = "紧急",   icon = "sf:exclamationmark.triangle"},
        {id = "review",    text = "待审核", icon = "sf:eye"},
        {id = "archive",   text = "归档",   icon = "sf:archivebox"},
    }

    local selected = app.chooser.show(tags, {
        title = "选择标签",
        placeholder = "搜索标签...",
        multiSelect = true
    })

    if selected then
        for _, tag in ipairs(selected) do
            app.log.info("选中标签: " .. tag.text)
        end
    end
end

注意事项

  1. 搜索过滤:自动匹配 textsubText 字段
  2. 阻塞调用:调用后等待用户选择或取消,阻塞当前线程
  3. 图标格式:使用 sf: 前缀加载 SF Symbols(如 sf:folder.fill
开发者文档
使用帮助
使用说明 脚本菜单 常见问题
脚本开发
开发指南
插件开发
快速开始 开发指南 示例插件
API 参考
概览 API 查询 插件信息 日志 Finder 上下文 插件设置 国际化
UI 与交互
对话框 进度条 系统通知 选择器 WebView 状态栏 Dock
文件与路径
文件操作 路径工具 Finder 操作 废纸篓 扩展属性 元数据 文件监听
数据格式
JSON Plist CSV XML PDF 图片
文本与编码
字符串 正则表达式 日期时间 颜色 加密编码
系统
Shell 命令 进程管理 应用管理 系统信息 AppleScript 快捷指令
系统信息
网络信息 电源/电池 屏幕/外观 音频控制 蓝牙设备 位置服务
网络
HTTP 请求 WebSocket URL 工具
输入与剪贴板
键盘模拟 鼠标模拟 全局热键 剪贴板 窗口管理
存储
SQLite Keychain UserDefaults
媒体
文字识别 二维码
工具
归档 类型标识 分享 定时器 防休眠 并发/协程