Tauri 2.0 macOS:在 Rust 中处理“Open With”并将文件传递给前端

Electro is a blazingly fast image viewer that was originally built with Rust & Tauri 2.0. Following a successful launch, many users requested macOS support so I began working on it. The first major hurdle I encountered is the focus of this blog post. Tauri 2.0’s documentation is not the best when it comes to niche issues like this so I ended up spending countless hours experimenting with different approaches. Hopefully, my experience will save others from the same trial-and-error process.

Electro 是一款极速的图像查看器,最初使用 Rust 和 Tauri 2.0 构建。在成功发布后,许多用户要求支持 macOS,于是我开始着手实现。我遇到的第一个重大障碍正是这篇博客的主题。Tauri 2.0 的文档在处理这类小众问题时并不完善,因此我花了无数小时尝试各种方法。希望我的经验能让其他人少走弯路。

To allow your Tauri 2.0 app to appear in macOS’s “Open With” menu, you need to configure the Info.plist file (plist = property list). This file can be created in your /src-tauri/Info.plist directory.

为了让你的 Tauri 2.0 应用出现在 macOS 的“用…打开”菜单中,你需要配置 Info.plist 文件(plist = property list)。该文件可以放在 /src-tauri/Info.plist 目录下。

The Info.plist file contains any metadata about your app which, in our case, will be used to include file associations. The main property you need in order to get file associations working is the CFBundleDocumentTypes property which lets macOS know exactly what file types your app supports. In the case of Electro, these will be image files but you can adjust this section to include any file type you want.

Info.plist 文件包含关于你应用的元数据,在我们的场景中将用于声明文件关联。要让文件关联生效,最关键的属性是 CFBundleDocumentTypes,它告诉 macOS 你的应用支持哪些文件类型。在 Electro 中,这些是图片文件,但你可以根据需要调整此部分以包含任何文件类型。

Here is a minimal example of what the CFBundleDocumentTypes section of your Info.plist file might look like:

下面是一个最小示例,展示了 Info.plist 文件中 CFBundleDocumentTypes 部分可能的样子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<key>CFBundleDocumentTypes</key>
<array>
 <dict>
 <key>CFBundleTypeName</key>
 <string>Image File</string>
 <key>LSItemContentTypes</key>
 <array>
 <string>public.png</string>
 <string>public.jpeg</string>
 <string>public.jpg</string>
 <string>public.gif</string>
 </array>
 <key>CFBundleTypeRole</key>
 <...
开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2025 iteam. Current version is 2.144.1. UTC+08:00, 2025-07-24 04:35
浙ICP备14020137号-1 $访客地图$