electron-builder双击文件打开关联应用(文件关联)

参考文章

配置文件关联

官方配置文档

 fileAssociations: [
          {
            ext: "ipc", // 关联文件扩展名 .ipc
            name: "IPS Config File", // 名称 默认为 ext
            description: "iConfig Machine Config File", // -仅限 Windows 文件描述
            icon: ipcicon, // 图标的路径 例如: build/iConfig/ipc/ipc 就会去读取 build/iConfig/ipc/ipc.ico (.icns对于 MacOS 和.ico Windows)
            role: "Editor", // String -仅限 macOS应用程序在类型方面的角色。该值可以是Editor、Viewer、Shell或None。对应于CFBundleTypeRole
          },
        ],

添加进程通信

// 主进程
win.on("ready-to-show", () => {
    // window 显示后
    logger.info("[ window ] ready-to-show");
    watchLauchFromIPC();
  });

/**
 * @description: 监视通过 ipc 文件启动
 * @param {*}
 * @return {*}
 */
function watchLauchFromIPC() {
  const argv = process.argv;
  // 不是 MacOS
  if (process.platform !== "darwin") {
    // argv[argv.length - 1] 为 ipc 文件路径
    const filePath = argv[argv.length - 1];
    if (/\.ipc$/.test(filePath)) {
      logger.info("[app] watchLauchFromIPC opne IPC File", filePath);
      bridge && bridge.win.webContents.send("launchFromIPC", filePath);
    }
  }
}
// 渲染进程
ipcRenderer.on("launchFromIPC", (e, filePath) => {
  logger.info("[ipcRenderer] launchFromIPC filePath = ", filePath);
  // TODO: 这里拿到文件路径就开始处理吧 
});

MacOS

MacOS 通过 open-file 获取到文件路径, 但 open-file 会在 appready 前触发, 所以直接模仿 win 的 process.argv, 将文件路径 push 到 process.argv

// 主进程 
// macos ipc 文件启动
app.on("will-finish-launching", () => {
  // custom schema handler on macos
  app.on("open-file", (event, url) => {
    event.preventDefault();
    logger.info("[app] open-file url", url);
    process.argv.push(url);
  });
})

修改 watchLauchFromIPC 兼容 MacOS

/**
 * @description: 监视通过 ipc 文件启动
 * @param {*}
 * @return {*}
 */
function watchLauchFromIPC() {
  const argv = process.argv;
  // argv[argv.length - 1] 为 ipc 文件路径
  const filePath = argv[argv.length - 1];
  if (/\.ipc$/.test(filePath)) {
    logger.info("[app] watchLauchFromIPC opne IPC File", filePath);
    bridge && bridge.win.webContents.send("launchFromIPC", filePath);
  }
}
posted @   __Bowen  阅读(645)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示