跳至主要内容
版本: v2.9.0

文件关联

文件关联功能允许您将特定文件类型与您的应用程序关联起来,以便当用户打开这些文件时,您的应用程序启动以处理它们。这对于文本编辑器、图像查看器或处理特定文件格式的任何应用程序特别有用。在本指南中,我们将逐步介绍在 Wails 应用程序中实现文件关联的步骤。

设置文件关联:

要设置文件关联,您需要修改应用程序的 wails.json 文件。在“info”部分添加一个“fileAssociations”部分,指定您的应用程序应关联的文件类型。

例如

{
"info": {
"fileAssociations": [
{
"ext": "wails",
"name": "Wails",
"description": "Wails Application File",
"iconName": "wailsFileIcon",
"role": "Editor"
},
{
"ext": "jpg",
"name": "JPEG",
"description": "Image File",
"iconName": "jpegFileIcon",
"role": "Editor"
}
]
}
}
属性描述
ext扩展名(不包括前导句点)。例如:png
name名称。例如:PNG 文件
iconName不带扩展名的图标名称。图标应位于 build 文件夹中。对于 macOS 和 Windows,将从 .png 文件生成合适的图标
description仅限 Windows。描述。它显示在 Windows 资源管理器中的“类型”列中。
role仅限 macOS。应用程序相对于类型的角色。对应于 CFBundleTypeRole。

平台特定:

macOS

当您使用应用程序打开文件(或文件)时,系统将启动您的应用程序并调用 Wails 应用程序中的 OnFileOpen 函数。示例

main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
Mac: &mac.Options{
OnFileOpen: func(filePaths []string) { println(filestring) },
},
Bind: []interface{}{
app,
},
})

if err != nil {
println("Error:", err.Error())
}
}

Windows

在 Windows 上,文件关联仅在使用 NSIS 安装程序时才受支持。在安装过程中,安装程序将为您的文件关联创建一个注册表项。当您使用应用程序打开文件时,应用程序的新实例将启动,并且文件路径将作为参数传递给您的应用程序。要处理此问题,您应该在应用程序中解析命令行参数。示例

main.go
func main() {
argsWithoutProg := os.Args[1:]

if len(argsWithoutProg) != 0 {
println("launchArgs", argsWithoutProg)
}
}

您还可以为您的应用程序启用单实例锁。在这种情况下,当您使用应用程序打开文件时,不会启动应用程序的新实例,并且参数将传递到正在运行的实例。有关详细信息,请查看单实例锁指南。示例

main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "e3984e08-28dc-4e3d-b70a-45e961589cdc",
OnSecondInstanceLaunch: app.onSecondInstanceLaunch,
},
Bind: []interface{}{
app,
},
})
}

Linux

目前,Wails 不支持为 Linux 打包。因此,您需要手动创建文件关联。例如,如果您将应用程序作为 .deb 包分发,则可以通过在捆绑包中添加所需的文件来创建文件关联。您可以使用 nfpm 为您的应用程序创建 .deb 包。

  1. 为您的应用程序创建一个 .desktop 文件,并在其中指定文件关联。示例
[Desktop Entry]
Categories=Office
Exec=/usr/bin/wails-open-file %u
Icon=wails-open-file.png
Name=wails-open-file
Terminal=false
Type=Application
MimeType=application/x-wails;application/x-test
  1. 创建 mime 类型文件。示例
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-wails">
<comment>Wails Application File</comment>
<glob pattern="*.wails"/>
</mime-type>
</mime-info>
  1. 为您的文件类型创建图标。推荐使用 SVG 图标。
  2. 为您的软件包准备 postInstall/postRemove 脚本。示例
# reload mime types to register file associations
update-mime-database /usr/share/mime
# reload desktop database to load app in list of available
update-desktop-database /usr/share/applications
# update icons
update-icon-caches /usr/share/icons/*
  1. 配置 nfpm 以使用您的脚本和文件。示例
name: "wails-open-file"
arch: "arm64"
platform: "linux"
version: "1.0.0"
section: "default"
priority: "extra"
maintainer: "FooBarCorp <[email protected]>"
description: "Sample Package"
vendor: "FooBarCorp"
homepage: "http://example.com"
license: "MIT"
contents:
- src: ../bin/wails-open-file
dst: /usr/bin/wails-open-file
- src: ./main.desktop
dst: /usr/share/applications/wails-open-file.desktop
- src: ./application-wails-mime.xml
dst: /usr/share/mime/packages/application-x-wails.xml
- src: ./application-test-mime.xml
dst: /usr/share/mime/packages/application-x-test.xml
- src: ../appicon.svg
dst: /usr/share/icons/hicolor/scalable/apps/wails-open-file.svg
- src: ../wailsFileIcon.svg
dst: /usr/share/icons/hicolor/scalable/mimetypes/application-x-wails.svg
- src: ../testFileIcon.svg
dst: /usr/share/icons/hicolor/scalable/mimetypes/application-x-test.svg
# copy icons to Yaru theme as well. For some reason Ubuntu didn't pick up fileicons from hicolor theme
- src: ../appicon.svg
dst: /usr/share/icons/Yaru/scalable/apps/wails-open-file.svg
- src: ../wailsFileIcon.svg
dst: /usr/share/icons/Yaru/scalable/mimetypes/application-x-wails.svg
- src: ../testFileIcon.svg
dst: /usr/share/icons/Yaru/scalable/mimetypes/application-x-test.svg
scripts:
postinstall: ./postInstall.sh
postremove: ./postRemove.sh
  1. 使用 nfpm 构建您的 .deb 包
nfpm pkg --packager deb --target .
  1. 现在,当您的软件包安装后,您的应用程序将与指定的文件类型关联。当您使用应用程序打开文件时,应用程序的新实例将启动,并且文件路径将作为参数传递给您的应用程序。要处理此问题,您应该在应用程序中解析命令行参数。示例
main.go
func main() {
argsWithoutProg := os.Args[1:]

if len(argsWithoutProg) != 0 {
println("launchArgs", argsWithoutProg)
}
}

您还可以为您的应用程序启用单实例锁。在这种情况下,当您使用应用程序打开文件时,不会启动应用程序的新实例,并且参数将传递到正在运行的实例。有关详细信息,请查看单实例锁指南。示例

main.go
func main() {
// Create application with options
err := wails.Run(&options.App{
Title: "wails-open-file",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "e3984e08-28dc-4e3d-b70a-45e961589cdc",
OnSecondInstanceLaunch: app.onSecondInstanceLaunch,
},
Bind: []interface{}{
app,
},
})
}