electron MaOS 文件关联优先级

electron MaOS 文件关联优先级
自己的electron软件希望能够称为某种类型如.txt文件,已通过electron-builder中实现了可选择自己的electron软件打开txt文件,但一直是另一个编辑器作为txt文件的默认打开方式,该如何从代码层面将自己的electron软件设置为更高的优先级呢?

https://blog.csdn.net/JianShengShuaiest/article/details/127819930

要将自己的 Electron 应用程序设置为 macOS 系统中特定文件类型的默认打开方式,您需要在应用程序中注册文档类型。具体步骤如下:

  1. 在 package.json 文件中添加 fileAssociations 属性,并为您的应用程序注册要关联的文件类型,例如 .txt 文件:
"build": {
  "fileAssociations": [
    {
      "ext": "txt",
      "name": "Text Document",
      "role": "Editor"
    }
  ]
}



  1. 使用 Electron 的 API app.setAsDefaultProtocolClient() 方法将您的应用程序注册为关联文件类型的默认打开方式。以下是示例代码
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

在 macOS 上,文件的默认打开方式由 Launch Services 维护。您可以通过调用 lsregister 工具来更改 Launch Services 数据库。不过,这种方法不够可靠,并且有可能会破坏其他应用程序的文件关联设置。

1、更好的方法是使用 Electron 的 API 来更改文件关联。您可以使用 app.setAsDefaultProtocolClient(protocol[, path, args]) 方法来将您的 Electron 应用程序注册为默认协议处理程序。在这种情况下,您可以使用 app.setAsDefaultProtocolClient('myapp') 将您的应用程序注册为 myapp: 协议的处理程序。同样,您可以使用 app.removeAsDefaultProtocolClient(protocol[, path, args]) 方法来删除协议处理程序的注册。

2、您还可以使用 app.setAsDefaultProtocolClientForFile(extension[, appName]) 方法来将您的 Electron 应用程序注册为默认的文件处理程序。在这种情况下,您可以使用 app.setAsDefaultProtocolClientForFile('txt', 'My App') 将您的应用程序注册为默认的 .txt 文件处理程序。

请注意,这些方法仅适用于您的应用程序的安装副本,它们不能更改用户的文件关联设置。如果用户希望将默认文件关联设置为您的应用程序,他们必须手动更改设置。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: Electron--electron-builder打包流程和下载不了包解决方法(window和Mac)中的 mac打包 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    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
    在这里插入图片描述
    如果还是不行就在拷贝一份到
    在这里插入图片描述


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

暂无解决方案,求大神指教