electron MaOS 文件关联优先级
自己的electron软件希望能够称为某种类型如.txt文件,已通过electron-builder中实现了可选择自己的electron软件打开txt文件,但一直是另一个编辑器作为txt文件的默认打开方式,该如何从代码层面将自己的electron软件设置为更高的优先级呢?
https://blog.csdn.net/JianShengShuaiest/article/details/127819930
要将自己的 Electron 应用程序设置为 macOS 系统中特定文件类型的默认打开方式,您需要在应用程序中注册文档类型。具体步骤如下:
"build": {
"fileAssociations": [
{
"ext": "txt",
"name": "Text Document",
"role": "Editor"
}
]
}
const { app } = require('electron')
// 注册文档类型
app.setAsDefaultProtocolClient('myapp', process.execPath, ['--'])
// 在应用程序启动时,检查是否存在命令行参数
if (process.argv.length >= 2) {
// 如果存在命令行参数,则将第二个参数作为文件路径
const filePath = process.argv[1]
// 处理文件
handleFile(filePath)
}
function handleFile(filePath) {
// 处理文件的逻辑
}
在上面的示例中,setAsDefaultProtocolClient() 方法将您的应用程序注册为 myapp 文件类型的默认打开方式。然后,在应用程序启动时,您可以检查命令行参数,获取要打开的文件路径,并执行相应的逻辑。
请注意,为了使应用程序能够在 macOS 上注册为默认打开方式,您还需要在应用程序的 Info.plist 文件中添加相关条目。这些条目包括 CFBundleDocumentTypes、UTExportedTypeDeclarations 等。您可以参考 Apple 的文档了解更多细节:https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_declare/understand_utis_declare.html
请注意,这些方法仅适用于您的应用程序的安装副本,它们不能更改用户的文件关联设置。如果用户希望将默认文件关联设置为您的应用程序,他们必须手动更改设置。
不知道你这个问题是否已经解决, 如果还没有解决的话:electron-builder --win --x64
运行以上命令会抛错误:
• electron-builder version=22.10.5 os=20.3.0
• writing effective config file=dist/builder-effective-config.yaml
• packaging platform=win32 arch=x64 electron=13.1.1 appOutDir=dist/win-unpacked
• Unpacking electron zip zipPath=undefined
• default Electron icon is used reason=application icon is not set
• downloading url=https://github.com/electron-userland/electron-builder-binaries/releases/download/wine-4.0.1-mac/wine-4.0.1-mac.7z size=19 MB parts=3
^C • canceling signal=interrupt
• cancelled by SIGINT
⨯ sha512 checksum mismatch, expected aCUQOyuPGlEvLMp0lPzb54D96+8IcLwmKTMElrZZqVWtEL1LQC7L9XpPv4RqaLX3BOeSifneEi4j9DpYdC1DCA==, got 1iqyfg0+72nqyW1/fDGvIoHUFVuANWYKMEBN3QCXuWb7o3L6Q5X2CxVx8ptFMiyasR9U+73kzxY84I4gihNHHA==
github.com/develar/app-builder/pkg/download.(*ActualLocation).concatenateParts
/Volumes/data/Documents/app-builder/pkg/download/ActualLocation.go:158
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadResolved
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:176
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadNoRetry
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:133
github.com/develar/app-builder/pkg/download.(*Downloader).Download
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:112
github.com/develar/app-builder/pkg/download.DownloadArtifact
/Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:107
github.com/develar/app-builder/pkg/wine.executeMacOsWine
/Volumes/data/Documents/app-builder/pkg/wine/wine.go:124
github.com/develar/app-builder/pkg/wine.ExecWine
/Volumes/data/Documents/app-builder/pkg/wine/wine.go:69
github.com/develar/app-builder/pkg/rcedit.editResources
/Volumes/data/Documents/app-builder/pkg/rcedit/rcedit.go:61
github.com/develar/app-builder/pkg/rcedit.ConfigureCommand.func1
/Volumes/data/Documents/app-builder/pkg/rcedit/rcedit.go:25
github.com/alecthomas/kingpin.(*actionMixin).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/actions.go:28
github.com/alecthomas/kingpin.(*Application).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:557
github.com/alecthomas/kingpin.(*Application).execute
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:390
github.com/alecthomas/kingpin.(*Application).Parse
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:222
main.main
/Volumes/data/Documents/app-builder/main.go:90
runtime.main
/usr/local/Cellar/go/1.15.5/libexec/src/runtime/proc.go:204
runtime.goexit
/usr/local/Cellar/go/1.15.5/libexec/src/runtime/asm_amd64.s:1374
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadResolved
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:178
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadNoRetry
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:133
github.com/develar/app-builder/pkg/download.(*Downloader).Download
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:112
github.com/develar/app-builder/pkg/download.DownloadArtifact
/Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:107
github.com/develar/app-builder/pkg/wine.executeMacOsWine
/Volumes/data/Documents/app-builder/pkg/wine/wine.go:124
github.com/develar/app-builder/pkg/wine.ExecWine
/Volumes/data/Documents/app-builder/pkg/wine/wine.go:69
github.com/develar/app-builder/pkg/rcedit.editResources
/Volumes/data/Documents/app-builder/pkg/rcedit/rcedit.go:61
github.com/develar/app-builder/pkg/rcedit.ConfigureCommand.func1
/Volumes/data/Documents/app-builder/pkg/rcedit/rcedit.go:25
github.com/alecthomas/kingpin.(*actionMixin).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/actions.go:28
github.com/alecthomas/kingpin.(*Application).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:557
github.com/alecthomas/kingpin.(*Application).execute
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:390
github.com/alecthomas/kingpin.(*Application).Parse
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:222
main.main
/Volumes/data/Documents/app-builder/main.go:90
runtime.main
/usr/local/Cellar/go/1.15.5/libexec/src/runtime/proc.go:204
runtime.goexit
/usr/local/Cellar/go/1.15.5/libexec/src/runtime/asm_amd64.s:1374
• Above command failed, retrying 3 more times
解决方法:
把downloading上的包下载下来放到
/Users/自己的电脑名称/Library/Caches/electron-builder/winCodeSign
如果还是不行就在拷贝一份到
暂无解决方案,求大神指教