File Association
File association feature allows you to associate specific file types with your app so that when users open those files, your app is launched to handle them. This can be particularly useful for text editors, image viewers, or any application that works with specific file formats. In this guide, we'll walk through the steps to implement file association in Wails app.
Set Up File Association:
To set up file association, you need to modify your application's wails.json file. In "info" section add a "fileAssociations" section specifying the file types your app should be associated with.
For example:
{
"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"
}
]
}
}
| Property | Description |
|---|---|
| ext | The extension (minus the leading period). e.g. png |
| name | The name. e.g. PNG File |
| iconName | The icon name without extension. Icons should be located in build folder. Proper icons will be generated from .png file for both macOS and Windows |
| description | Windows-only. The description. It is displayed on the Type column on Windows Explorer. |
| role | macOS-only. The app’s role with respect to the type. Corresponds to CFBundleTypeRole. |
Platform Specifics:
macOS
When you open file (or files) with your app, the system will launch your app and call the OnFileOpen function in your Wails app. Example:
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())
}
}