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
会在 app
的 ready
前触发, 所以直接模仿 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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能