Electron app auto launch shows an additional window due to incorrect app path

Viewed 3154

I'm using the app.setLoginItemSettings(settings) for auto launching the app when system starts. The example given in the Electron API doc page is for Windows I believe. When adding the following in the Electron main.js, the app auto launches just fine but also shows an additional window that says the following which I don't want:

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

enter image description here

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"`
    ]
  });
});

I also tried with the following path but that also didn't work:

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"`
    ]
  });
});

In addition, also tried with the Electron Root Path package but still no luck:

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"`
    ]
  });
});

Last but not least , also tried with the following but still no luck:

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"`
      ]
    });
  }
});

I am out of all the ideas I have to make this work. Any help from anyone would be really appreciated :)

1 Answers

I finally figured it out after hours of investigation. The above code was just working fine from the beginning. However, somehow the MyApp.app located in the /Applications and the one I was working in my code both was executing after system starts. So, the local project (development project) was showing the additional window as it wasn't finding the correct app path.

So, I disabled the app launch on development mode and only run it on production mode using the following:

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"`
      ]
    });
  }
}
Related