|
| 1 | +# macOS Finder Quick Actions Contract |
| 2 | + |
| 3 | +## Scenario: Finder 快捷操作通用分派器 |
| 4 | + |
| 5 | +### 1. Scope / Trigger |
| 6 | + |
| 7 | +- Trigger: 在 `macos/quick-actions/` 下新增或修改 Finder 右键快捷操作、Automator workflow、`~/Library/Services/*.workflow` 安装脚本或动作分派逻辑。 |
| 8 | +- 目标:workflow 只作为 Finder UI 入口,业务逻辑放在仓库脚本中,方便验证、复用和后续替换为 Go 二进制 runner。 |
| 9 | +- 目录名说明:`~/Library/Services` 是 macOS 历史安装位置;Finder UI 可能显示为“快捷操作”或“服务”,文档应同时覆盖这两个入口名称。 |
| 10 | + |
| 11 | +### 2. Signatures |
| 12 | + |
| 13 | +- 安装入口: |
| 14 | + |
| 15 | +```zsh |
| 16 | +zsh macos/08installQuickActions.zsh [--dry-run] [--uninstall] |
| 17 | +``` |
| 18 | + |
| 19 | +- 通用分派器: |
| 20 | + |
| 21 | +```zsh |
| 22 | +zsh macos/quick-actions/run.zsh <action-id> [path...] |
| 23 | +``` |
| 24 | + |
| 25 | +- 动作脚本示例: |
| 26 | + |
| 27 | +```zsh |
| 28 | +zsh macos/quick-actions/fix-app-open-issue.zsh [--dry-run] <app-path>... |
| 29 | +``` |
| 30 | + |
| 31 | +### 3. Contracts |
| 32 | + |
| 33 | +- workflow contract: |
| 34 | + - workflow 文件放在 `macos/quick-actions/<Name>.workflow/`。 |
| 35 | + - 安装脚本复制 workflow 到 `~/Library/Services/<Name>.workflow`。 |
| 36 | + - workflow 的 Run Shell Script 只负责调用 `run.zsh <action-id> "$@"`。 |
| 37 | + - workflow 内部不要内联业务逻辑,不要直接调用具体动作脚本。 |
| 38 | +- runner contract: |
| 39 | + - 第一个参数必须是 action id。 |
| 40 | + - 未知 action id 返回 2,并输出可读错误。 |
| 41 | + - 已知 action id 透传剩余参数到对应动作脚本。 |
| 42 | +- action contract: |
| 43 | + - 动作脚本必须自行校验 Finder 传入路径。 |
| 44 | + - 非目标类型输入必须安全跳过,不执行破坏性操作。 |
| 45 | + - 公共函数必须带中文注释,说明功能、入参和返回值。 |
| 46 | + |
| 47 | +### 4. Validation & Error Matrix |
| 48 | + |
| 49 | +| 条件 | 期望行为 | |
| 50 | +|------|----------| |
| 51 | +| `run.zsh` 缺少 action id | 返回 2,输出 usage | |
| 52 | +| `run.zsh` 收到未知 action id | 返回 2,输出未知动作和 usage | |
| 53 | +| workflow 未安装 | `06verifyInstall.zsh --step quick-actions` 失败并提示运行安装脚本 | |
| 54 | +| workflow 命令未指向 `run.zsh` 和 action id | quick-actions 验证失败 | |
| 55 | +| 传入非 `.app` 到 `fix-app-open-issue` | 安全跳过,返回成功 | |
| 56 | +| 传入 `.app` 到 `fix-app-open-issue` | 输出 `spctl`、`codesign`、quarantine 状态,再按安全边界处理 | |
| 57 | + |
| 58 | +### 5. Good/Base/Bad Cases |
| 59 | + |
| 60 | +- Good: `workflow -> run.zsh fix-app-open-issue "$@" -> fix-app-open-issue.zsh "$@"`,业务逻辑可独立命令行验证。 |
| 61 | +- Base: 安装脚本支持 `--dry-run` 和重复安装,不删除用户其它 Services。 |
| 62 | +- Bad: 每个 workflow 都复制一段不同的 AppleScript、shell quote 和业务逻辑,后续修 bug 时需要逐个同步。 |
| 63 | + |
| 64 | +### 6. Tests Required |
| 65 | + |
| 66 | +- 静态检查: |
| 67 | + |
| 68 | +```zsh |
| 69 | +zsh -n macos/08installQuickActions.zsh macos/quick-actions/run.zsh macos/quick-actions/*.zsh |
| 70 | +plutil -lint "macos/quick-actions/Fix App Open Issue.workflow/Contents/Info.plist" |
| 71 | +plutil -lint "macos/quick-actions/Fix App Open Issue.workflow/Contents/document.wflow" |
| 72 | +``` |
| 73 | + |
| 74 | +- 行为检查: |
| 75 | + |
| 76 | +```zsh |
| 77 | +zsh macos/08installQuickActions.zsh --dry-run |
| 78 | +zsh macos/08installQuickActions.zsh |
| 79 | +zsh macos/06verifyInstall.zsh --step quick-actions |
| 80 | +zsh macos/quick-actions/run.zsh fix-app-open-issue --dry-run /tmp/not-an-app |
| 81 | +``` |
| 82 | + |
| 83 | +- AppleScript quoting 检查: |
| 84 | + |
| 85 | +```zsh |
| 86 | +osascript - "/tmp/a path/run.zsh" "fix-app-open-issue" "/tmp/My App.app" <<'APPLESCRIPT' |
| 87 | +on run argv |
| 88 | + if (count of argv) < 2 then return |
| 89 | + set commandText to "/bin/zsh " & quoted form of (item 1 of argv) & " " & quoted form of (item 2 of argv) |
| 90 | + repeat with i from 3 to count of argv |
| 91 | + set commandText to commandText & " " & quoted form of (item i of argv) |
| 92 | + end repeat |
| 93 | + return commandText |
| 94 | +end run |
| 95 | +APPLESCRIPT |
| 96 | +``` |
| 97 | +
|
| 98 | +### 7. Wrong vs Correct |
| 99 | +
|
| 100 | +#### Wrong |
| 101 | +
|
| 102 | +```applescript |
| 103 | +on shellQuote(value) |
| 104 | + set AppleScript's text item delimiters to "'\\''" |
| 105 | + ... |
| 106 | +end shellQuote |
| 107 | +``` |
| 108 | +
|
| 109 | +这种手写 quote 容易在 Automator 的 Run Shell Script 字符串中触发 AppleScript 语法错误,例如“预期是 `"`,却找到未知的记号”。 |
| 110 | +
|
| 111 | +#### Correct |
| 112 | +
|
| 113 | +```applescript |
| 114 | +set commandText to "/bin/zsh " & quoted form of (item 1 of argv) & " " & quoted form of (item 2 of argv) |
| 115 | +repeat with i from 3 to count of argv |
| 116 | + set commandText to commandText & " " & quoted form of (item i of argv) |
| 117 | +end repeat |
| 118 | +``` |
| 119 | +
|
| 120 | +AppleScript 自带 `quoted form of`,应优先使用它生成 shell 参数;workflow 内只拼 runner 和 action id,复杂逻辑放到仓库脚本中。 |
0 commit comments