Thanks to visit codestin.com
Credit goes to lib.rs

#tui #macos #associations #manage #file

app openwith

A TUI application to manage file associations on macOS

1 unstable release

0.1.0 Feb 26, 2026

#1065 in Configuration

MIT license

50KB
1K SLoC

OpenWith

一个用于管理 macOS 文件关联的 TUI(终端用户界面)应用程序。厌倦了文件总是默认用 Xcode 打开?OpenWith 可以帮你轻松管理和设置默认的文件打开程序。

功能特性

  • 🎯 智能文件关联管理 - 通过 TOML 配置文件管理文件扩展名与应用程序的关联
  • 🖥️ 直观的 TUI 界面 - 使用方向键和快捷键的现代化终端界面
  • 🔄 交互式应用选择 - 当没有预设关联时,提供可用应用程序列表供选择
  • 快速文件打开 - 支持命令行直接打开文件或启动 TUI 界面
  • 🛠️ 系统集成 - 直接与 macOS Launch Services 交互,真正改变系统默认设置

安装

前置条件

  • macOS 系统
  • Rust 1.70+ (如果从源码编译)
  • duti 命令行工具 (推荐安装以获得最佳体验)

安装 duti (推荐)

为了获得最佳的文件关联管理体验,建议安装 duti

# 使用 Homebrew 安装
brew install duti

# 或使用 MacPorts
sudo port install duti

从源码编译

git clone <你的仓库>
cd openwith
cargo build --release

编译完成后,可执行文件位于 target/release/openwith

使用方法

1. 启动 TUI 界面

./target/release/openwith
# 或者
./target/release/openwith tui

2. 直接打开文件

# 如果已配置默认应用,直接打开
./target/release/openwith ~/Documents/test.rs

# 如果未配置,会显示应用选择界面
./target/release/openwith ~/Documents/unknown.xyz

3. 命令行设置关联

# 设置 .rs 文件默认用 VS Code 打开
./target/release/openwith set .rs com.microsoft.VSCode

# 查看当前 .py 文件的默认应用
./target/release/openwith get .py

配置文件

配置文件位于 ~/.config/openwith/config.toml,首次运行时会自动创建:

[settings]
# 选择应用后是否询问设为默认
ask_before_setting_default = true

# 在应用选择器中显示 Bundle ID
show_bundle_ids = true

# 首选应用列表(会优先显示)
preferred_apps = [
    "com.microsoft.VSCode",
    "com.sublimetext.4", 
    "com.apple.TextEdit",
    "com.jetbrains.intellij",
]

# 自定义文件关联
[file_associations]
".rs" = "com.microsoft.VSCode"
".py" = "com.microsoft.VSCode" 
".txt" = "com.apple.TextEdit"
".md" = "com.microsoft.VSCode"

TUI 界面操作

主菜单

  • / - 导航菜单项
  • Enter - 选择菜单项
  • q - 退出程序

应用选择器

  • / - 浏览可用应用程序
  • Enter - 选择应用程序打开文件
  • s - 设置为默认应用程序
  • b - 返回上级菜单

文件关联管理

  • / - 浏览现有关联
  • d - 删除选中的关联
  • a - 添加新关联
  • b - 返回主菜单

工作原理

  1. 配置优先级: 程序首先检查本地配置文件中的关联设置
  2. 系统查询: 如果没有本地配置,查询 macOS Launch Services 获取系统默认设置
  3. 用户选择: 如果没有任何设置,显示所有可用应用程序供用户选择
  4. 持久化存储: 用户的选择可以保存到配置文件,并更新系统默认设置

故障排除

duti 未安装

如果没有安装 duti,程序会自动退回到使用原生 macOS 命令。建议安装 duti 以获得更好的体验:

brew install duti

Launch Services 权限

首次使用时,macOS 可能会请求权限来修改文件关联。请在系统偏好设置中允许相关权限。

配置文件问题

如果配置文件损坏,删除 ~/.config/openwith/config.toml 重新启动程序即可重新生成。

找不到应用程序

如果程序无法找到可用的应用程序,请确保:

  • 相关应用程序已正确安装
  • 应用程序的 Info.plist 文件包含正确的文件类型关联信息

开发

项目结构:

src/
├── main.rs          # 主入口和命令行参数解析
├── config.rs        # TOML 配置文件管理
├── macos.rs         # macOS Launch Services 交互
├── cli.rs           # TUI 应用主逻辑
└── ui/              # UI 组件模块
    ├── mod.rs
    ├── app.rs       # 应用状态管理
    ├── main_menu.rs # 主菜单界面
    └── selector.rs  # 应用选择界面

运行测试

cargo test

开发模式运行

cargo run -- tui

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

Dependencies

~9–22MB
~240K SLoC