写一个插件

Minibar 插件(MiniApp)是一个包含 manifest.json 和一个 JavaScript 文件的文件夹。它在独立进程的沙箱化 JavaScriptCore 虚拟机中运行,渲染一棵小小的声明式界面树,并可以为菜单栏提名一行文字。

  1. 脚手架

    在仓库检出目录里执行。命令行工具会校验 manifest、用真实运行时渲染一次,并打包成可分发的 zip。

    Terminal
    swift run minibar-cli init ./hello --id com.example.hello --name "Hello"
    swift run minibar-cli validate ./hello
    node tools/jsharness/run.js ./hello      # render once with a mock host, any OS
    swift run minibar-cli pack ./hello --out hello.zip
  2. 写 render() 与 actions

    render(ctx) 返回菜单栏标题和一组界面节点。按钮触发 action;ctx.http、ctx.storage 等宿主调用都是同步的,并经过用户授予的权限。

    hello/main.js
    // hello/main.js
    defineMiniApp({
      render(ctx) {
        const count = ctx.storage.get('count', 0);
        return {
          title: { text: 'Hello ×' + count, icon: 'hand.wave' },
          body: [
            ctx.ui.text('Rendered at ' + ctx.now().toLocaleTimeString(ctx.locale)),
            ctx.ui.button('Bump', 'bump'),
          ],
        };
      },
      actions: {
        bump(ctx) { ctx.storage.set('count', ctx.storage.get('count', 0) + 1); },
      },
    });
  3. 链接文件夹

    设置 › 插件 › 链接插件文件夹…(菜单栏右键菜单里也有)。Minibar 会监视这个文件夹,每次保存自动重载插件,保留它的数据、设置和授权。

  4. 发布并提交

    把文件夹发布到 npm(npm publish --access public;package.json 里没有任何会执行的东西)。然后申请收录:这个精确版本会被扫描、在模拟宿主下渲染并人工审核,之后签名索引锁定它的名称、版本与哈希。

    Terminal
    node tools/jsharness/scan.js --strict ./hello    # the registry rules, including package.json
    node tools/jsharness/run.js ./hello --locale zh-CN
    npm publish --access public                      # the exact tarball the index will pin

插件能申请什么

每项权限都在 manifest 中声明,安装前逐条解释,可在设置中随时撤销。这里没列出的能力就不存在:没有 shell、没有任意文件、没有 AppleScript。

权限允许做什么
网络
network
从插件声明的允许域名获取数据。
通知
notifications
显示通知(每分钟最多一条)。
日历
calendar
读取你即将到来的日历日程;在你点按钮时添加日程。
提醒事项
reminders
读取你未完成的提醒事项;在你点按钮时添加或完成一条。
定位
location
使用你的大致位置。
剪贴板
clipboard
读取剪贴板上的当前文本。
文件
files
读取你在文件对话框中选择的文件和文件夹。仅限你选中的内容;可随时在插件设置中移除。
系统操作
system
释放内存、运行你的快捷指令或保持 Mac 唤醒,仅在你点击插件中的按钮时执行。
其他 App
apps
查看正在运行的 App;在你点击按钮时打开 App 或 manifest 中声明的 URL scheme。