由于应用程序路径不正确,Electron 应用程序自动启动会显示一个额外的窗口

Md *_*han 3 javascript node.js electron

我正在使用app.setLoginItemSettings(settings)for 在系统启动时自动启动应用程序。Electron API 文档页面中给出的示例适用于我相信的 Windows。在 Electron 中添加以下内容时main.js,应用程序自动启动就好了,但还会显示一个额外的窗口,上面写着我不想要的以下内容:

To run a local app, execute the following on the command line:
Users/abc/Documents/Repositories/app/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron path-to-app
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

main.js

const appFolder = path.dirname(process.execPath)
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
const exeName = path.basename(process.execPath)

import { app } from "electron";

app.on("ready", async() => {
  app.setLoginItemSettings({
    openAtLogin: true,
    path: updateExe,
    args: [
      '--processStart', `"${exeName}"`,
      '--process-start-args', `"--hidden"`
    ]
  });
});
Run Code Online (Sandbox Code Playgroud)

我也尝试过以下操作,path但这也不起作用:

const appFolder = path.dirname(process.execPath)
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
const exeName = path.basename(process.execPath)

import { app } from "electron";

app.on("ready", async() => {
  app.setLoginItemSettings({
    openAtLogin: true,
    path: "/Applications/MyApp.app",
    args: [
      '--processStart', `"${exeName}"`,
      '--process-start-args', `"--hidden"`
    ]
  });
});
Run Code Online (Sandbox Code Playgroud)

此外,还尝试了Electron Root Path 包,但仍然没有运气:

const appFolder = path.dirname(process.execPath)
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
const exeName = path.basename(process.execPath)

import { app } from "electron";
import { rootPath } from "electron-root-path";

app.on("ready", async() => {
  app.setLoginItemSettings({
    openAtLogin: true,
    path: rootPath,
    args: [
      '--processStart', `"${exeName}"`,
      '--process-start-args', `"--hidden"`
    ]
  });
});
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要的,也尝试了以下但仍然没有运气:

const appFolder = path.dirname(process.execPath)
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
const exeName = path.basename(process.execPath)

import { app } from "electron";
import { rootPath } from "electron-root-path";

app.on("ready", async() => {
if (process.platform === "darwin") {
    app.setLoginItemSettings({
      openAtLogin: true,
      openAsHidden: true,
      path: rootPath
    });
  } else {
    app.setLoginItemSettings({
      openAtLogin: true,
      openAsHidden: true,
      path: updateExe,
      args: [
        "--processStart",
        `"${exeName}"`,
        "--process-start-args",
        `"--hidden"`
      ]
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

我没有完成这项工作的所有想法。任何人的任何帮助将不胜感激:)

Md *_*han 5

经过几个小时的调查,我终于弄清楚了。上面的代码从一开始就运行良好。但是,不知何故,MyApp.app位于/Applications和我在我的代码中工作的那个都在系统启动后执行。因此,本地项目(开发项目)显示了附加窗口,因为它没有找到正确的应用程序路径。

因此,我在开发模式下禁用了应用程序启动,并且仅使用以下命令在生产模式下运行它:

import path from "path";
const appFolder = path.dirname(process.execPath);
const updateExe = path.resolve(appFolder, "..", "Update.exe");
const exeName = path.basename(process.execPath);

const isDevelopment = process.env.NODE_ENV !== "production";

app.on("ready", async () => {
  if (!isDevelopment) launchAtStartup();
}

function launchAtStartup() {
  if (process.platform === "darwin") {
    app.setLoginItemSettings({
      openAtLogin: true,
      openAsHidden: true
    });
  } else {
    app.setLoginItemSettings({
      openAtLogin: true,
      openAsHidden: true,
      path: updateExe,
      args: [
        "--processStart",
        `"${exeName}"`,
        "--process-start-args",
        `"--hidden"`
      ]
    });
  }
}
Run Code Online (Sandbox Code Playgroud)