场景 033 · Codex Use Case

构建 macOS 应用

构建 SwiftUI Mac 应用,建立命令行优先的构建运行循环,再逐步加入原生场景、窗口、AppKit 和签名流程。

macOSCode
Build for macOSmacOSCode

原文核对:2026-10-13 · 中文正文 · 英文提示词与演示

场景定位

构建 SwiftUI Mac 应用,建立命令行优先的构建运行循环,再逐步加入原生场景、窗口、AppKit 和签名流程。

难度
高级
时间跨度
1 小时

适合用于

  • 从零搭建原生桌面框架和可重复构建脚本的 SwiftUI 项目。
  • 涉及窗口、菜单、侧栏、设置、AppKit 或签名的现有 Mac 应用。
  • 希望命令行优先,同时遵循原生桌面 UX 的团队。

相关工具

Build macOS Apps

通过 shell 构建调试、设计原生 SwiftUI 场景和窗口,按需桥接 AppKit,并准备签名和公证。

Build macOS Apps ↗

起步提示词

英文原文 · 可复制后按实际材料修改

Use the Build macOS Apps plugin to scaffold a starter macOS SwiftUI app and add a project-local `script/build_and_run.sh` entrypoint I can wire to a `Run` action.

Constraints:
- Stay shell-first. Prefer `xcodebuild` for Xcode projects and `swift build` for package-first apps.
- Model Mac scenes explicitly with a main window plus `Settings`, `MenuBarExtra`, or utility windows only when they fit the product.
- Prefer desktop-native sidebars, toolbars, menus, keyboard shortcuts, and system materials over iOS-style push navigation.
- Use a narrow AppKit bridge only when SwiftUI cannot express the desktop behavior cleanly.
- Keep one small validation loop for each change and tell me exactly which build, launch, or log commands you ran.

Deliver:
- the app scaffold or requested Mac feature slice
- a reusable build-and-run script
- the smallest validation steps you ran
- any desktop-specific follow-up work you recommend

搭建应用和构建循环

新 Mac 应用先选择场景模型:WindowGroup、Window、Settings、MenuBarExtra 或 DocumentGroup,从首版遵循桌面结构。

Xcode 项目使用 xcodebuild;以包为主的应用使用 swift build 和项目内 script/build_and_run.sh,停止旧进程、构建、启动新产物,并可输出日志或遥测。

纯 SwiftPM GUI 应用应打包成 .app 启动,不直接运行裸可执行文件,以免验证时缺失 Dock、激活或 bundle 身份行为。

使用技能

桌面专属工作增加时,引入 Build macOS Apps 插件:包含 shell 构建调试、SwiftPM 打包、SwiftUI 场景窗口、AppKit、统一日志、测试分诊及签名公证。

安装和使用方式参阅插件与技能文档。

构建桌面原生 UI

遵循 Mac 约定:NavigationSplitView 用于侧栏与详情,Settings 用于偏好,工具栏和命令提供可发现操作,菜单栏扩展用于轻量常驻工具。

先使用系统材质、语义颜色和标准控件;产品确需独特桌面表现时,再加入自定义窗口、拖动区域或 Liquid Glass。

SwiftUI 无法满足时仅添加最小 AppKit 桥接,例如打开保存面板、第一响应者控制、菜单验证、拖放边缘情况或专用 NSView。

调试、测试和发布准备

在窗口打开、侧栏选择、菜单命令或后台同步周围加入少量 Logger 事件,启动后用 log stream 验证。

测试失败时先运行最小有效的 xcodebuild test 或 swift test 范围,区分编译、断言、崩溃、偶发失败或环境问题。

进入分发阶段时,同时准备 Xcode 手动归档和脚本化归档、公证路径;用 codesign、plutil 检查应用包、entitlements 和 hardened runtime,通过 App Store Connect CLI 保持终端上传。

提示词示例

官方提示词

英文原文 · 可复制后按实际材料修改

Use the Build macOS Apps plugin to build a native macOS SwiftUI version of this app feature.

Constraints:

- Use a desktop-native scene structure with a main window, settings, and toolbar/command actions where they make sense.
- Prefer a sidebar/detail layout over iOS-style push navigation if the feature benefits from always-visible structure.
- Add a tiny AppKit bridge only if SwiftUI cannot express one specific desktop behavior cleanly.
- Create or update `script/build_and_run.sh` so the build/run loop stays shell-first.
- Tell me which build, launch, test, and log commands you used.

Ship the feature slice, verify it with the smallest relevant build or test loop, and summarize any signing or packaging follow-up needed before distribution.

实用建议

明确场景

主窗口、设置、工具窗口和菜单栏扩展分别建成场景根,不把全部应用塞进巨大视图。

优先利用系统界面

自定义侧栏、工具栏或材质前,先检查标准 SwiftUI 场景和窗口 API 是否已满足需求。

缩小 AppKit 边界

用 NSViewRepresentable、NSViewControllerRepresentable 或小型 NSWindow 辅助代码补足具体能力,选择和应用状态仍由 SwiftUI 管理。

单独验证签名和公证

本地启动成功不证明签名或公证就绪。保留手动 Xcode 归档作单次发布检查,另建脚本化路径供重复分发;发布任务中运行 codesign 和 plutil 检查。

技术栈

需要默认选项用途
UI 框架SwiftUISwiftUI ↗适合窗口、侧栏、工具栏、设置和场景驱动结构。
AppKit 桥接AppKitAppKit ↗用小型 NSViewRepresentable、NSViewControllerRepresentable 或 NSWindow 桥接补足桌面行为。
构建与打包xcodebuild、swift build、App Store Connect CLIApp Store Connect CLI ↗在可重复的终端流程中完成构建、归档、公证和 App Store 上传。
展开英文原文对照

Use Codex to build macOS SwiftUI apps, wire a shell-first build-and-run loop, and add desktop-native scene, window, AppKit, and signing workflows as the app matures.

Scaffold the app and build loop

For a new Mac app, ask Codex to choose the right scene model first: WindowGroup, Window, Settings, MenuBarExtra, or DocumentGroup. That keeps the app desktop-native from the first pass instead of growing from an iOS-style ContentView.

Keep the execution loop shell-first. For Xcode projects, use xcodebuild. For package-first apps, use swift build and a project-local script/build_and_run.sh wrapper that stops the old process, builds the app, launches the new artifact, and can optionally expose logs or telemetry.

If a pure SwiftPM app is a GUI app, bundle and launch it as a .app instead of running the raw executable directly. That avoids missing Dock, activation, and bundle-identity issues during local validation.

Leverage skills

Add the Build macOS Apps plugin once the work gets more desktop-specific. It covers shell-first build and debug loops, SwiftPM app packaging, native SwiftUI scene and window patterns, AppKit interop, unified logging, test triage, and signing/notarization workflows.

To learn more about how to install and use plugins and skills, see the plugins documentation and skills documentation.

Build desktop-native UI

Prefer Mac conventions over iOS navigation patterns. Use NavigationSplitView for sidebar/detail layouts, explicit Settings scenes for preferences, toolbars and commands for discoverable actions, and menu bar extras for lightweight always-available utilities.

Use system materials, semantic colors, and standard controls first. Add custom window styling, drag regions, or Liquid Glass surfaces only when the product needs a distinct desktop surface.

If SwiftUI gets close but not all the way there, add the smallest possible AppKit bridge. Good examples are open/save panels, first-responder control, menu validation, drag-and-drop edges, and a wrapped NSView for one specialized control.

Debug, test, and prepare for shipping

For runtime behavior, ask Codex to add a few Logger events around window opening, sidebar selection, menu commands, or background sync, then verify those events with log stream after the app launches.

For failing tests, have Codex run the smallest useful xcodebuild test or swift test scope first and classify whether the issue is compilation, an assertion failure, a crash, a flake, or an environment/setup problem.

When the work shifts from local iteration to distribution, ask Codex to prepare both a manual archive path in Xcode and a script-based archive and notarization path for repeatable shipping. Have it inspect the app bundle, entitlements, and hardened runtime with codesign and plutil, and use App Store Connect CLI when you want uploads to stay in the terminal too.

Example prompt

官方提示词

英文原文 · 可复制后按实际材料修改

Use the Build macOS Apps plugin to build a native macOS SwiftUI version of this app feature.

Constraints:

- Use a desktop-native scene structure with a main window, settings, and toolbar/command actions where they make sense.
- Prefer a sidebar/detail layout over iOS-style push navigation if the feature benefits from always-visible structure.
- Add a tiny AppKit bridge only if SwiftUI cannot express one specific desktop behavior cleanly.
- Create or update `script/build_and_run.sh` so the build/run loop stays shell-first.
- Tell me which build, launch, test, and log commands you used.

Ship the feature slice, verify it with the smallest relevant build or test loop, and summarize any signing or packaging follow-up needed before distribution.

Practical tips

Keep scenes explicit

Model the main window, settings window, utility windows, and menu bar extras as separate scene roots instead of hiding the whole app inside one giant view.

Let system chrome do more of the work

Before creating custom sidebars, toolbars, or materials, check whether standard SwiftUI scene and window APIs already give you the Mac behavior you want.

Treat AppKit as a narrow edge

Use NSViewRepresentable, NSViewControllerRepresentable, or a focused NSWindow helper for one missing desktop capability, but keep SwiftUI as the source of truth for selection and app state.

Validate signing and notarization separately from local build success

A successful local launch does not prove the app is signed or notarization-ready. Keep a manual Xcode archive flow for one-off release checks, add a scripted archive and notarization flow for repeatable distribution, and run codesign and plutil checks when the task is about shipping, not just local iteration.