File Associations
이 콘텐츠는 아직 번역되지 않았습니다.
Relevant Platforms: Windows macOS
File associations allow your application to handle specific file types when users open them. This is particularly useful for text editors, image viewers, or any application that works with specific file formats. This guide explains how to implement file associations in your Wails v3 application.
Overview
Section titled “Overview”File association support in Wails v3 is currently available for:
- Windows (NSIS installer packages)
- macOS (application bundles)
Configuration
Section titled “Configuration”File associations are configured in the config.yml file located in your
project’s build directory.
Basic Configuration
Section titled “Basic Configuration”To set up file associations:
- Open
build/config.yml - Add your file associations under the
fileAssociationssection - Run
wails3 update build-assetsto update the build assets - Set the
FileAssociationsfield in the application options - Package your application using
wails3 package
Here’s an example configuration:
fileAssociations: - ext: myapp name: MyApp Document description: MyApp Document File iconName: myappFileIcon role: Editor - ext: custom name: Custom Format description: Custom File Format iconName: customFileIcon role: EditorConfiguration Properties
Section titled “Configuration Properties”| Property | Description | Platform |
|---|---|---|
| ext | File extension without the leading period (e.g., txt) | All |
| name | Display name for the file type | All |
| description | Description shown in file properties | Windows |
| iconName | Name of the icon file (without extension) in the build folder | All |
| role | Application’s role for this file type (e.g., Editor, Viewer) | macOS |
| mimeType | MIME type for the file (e.g., image/jpeg) | macOS |
Listening for File Open Events
Section titled “Listening for File Open Events”To handle file open events in your application, you can listen for the
events.Common.ApplicationOpenedWithFile event:
package main
import ( "github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/events")
func main() { app := application.New(application.Options{ Name: "MyApp", FileAssociations: []string{".txt", ".md"}, // Specify supported extensions })
// Listen for files being used to open the application app.Event.OnApplicationEvent(events.Common.ApplicationOpenedWithFile, func(event *application.ApplicationEvent) { associatedFile := event.Context().Filename() app.Dialog.Info().SetMessage("Application opened with file: " + associatedFile).Show() })
// Create your window and run the app...}Step-by-Step Tutorial
Section titled “Step-by-Step Tutorial”Let’s walk through setting up file associations for a simple text editor:
-
Create Icons
Section titled “Create Icons”- Create icons for your file type (recommended sizes: 16x16, 32x32, 48x48, 256x256)
- Save the icons in your project’s
buildfolder - Name them according to your
iconNameconfiguration (e.g.,textFileIcon.png)
- For macOS add copy statement like
cp build/darwin/documenticon.icns {{.BIN_DIR}}/{{.APP_NAME}}.app/Contents/Resourcesin thecreate:app:bundle:task.
-
Configure File Associations
Section titled “Configure File Associations”Edit the
build/config.ymlfile to add your file associations:build/config.yml fileAssociations:- ext: txtname: Text Documentdescription: Plain Text DocumenticonName: textFileIconrole: Editor -
Update Build Assets
Section titled “Update Build Assets”Run the following command to update the build assets:
Terminal window wails3 update build-assets -
Set File Associations in the Application Options
Section titled “Set File Associations in the Application Options”In your
main.gofile, set theFileAssociationsfield in the application options:app := application.New(application.Options{Name: "MyApp",FileAssociations: []string{".txt", ".md"}, // Specify supported extensions}) -
Package Your Application
Section titled “Package Your Application”Package your application using the following command:
Terminal window wails3 packageThe packaged application will be created in the
bindirectory. You can then install and test the application.Additional Notes
Section titled “Additional Notes”- Icons should be provided in PNG format in the build folder
- Testing file associations requires installing the packaged application