Git Product home page Git Product logo

Comments (13)

stenya avatar stenya commented on July 20, 2024 2

To summarize the comment from @newCodeRunner, this snipped works on Windows\Linux\macOS (Electron v12.0.0).
Not even necessary to call app.setLoginItemSettings(...)

import { app } from "electron";
var AutoLaunch = require("auto-launch");

let launcherOptions = { name: "AppName", isHidden: true }; // isHidden is in use by Windows and Linux implementation (see function: WasOpenedAtLogin())
let autoLauncher = new AutoLaunch(launcherOptions);

...

export function WasOpenedAtLogin() {
  try {
    if (Platform() === PlatformEnum.macOS) {
      let loginSettings = app.getLoginItemSettings();
      return loginSettings.wasOpenedAtLogin;
    }
    return app.commandLine.hasSwitch("hidden");
  } catch {
    return false;
  }
}

export async function AutoLaunchSet(isEnabled) {
  if (isEnabled) await autoLauncher.enable();
  else await autoLauncher.disable();
}

from node-auto-launch.

Noitidart avatar Noitidart commented on July 20, 2024 1

I didn't find a solution. :(

from node-auto-launch.

Connum avatar Connum commented on July 20, 2024 1

A year and a half later and not having worked much with Electon since, all I can say is that I had it working with my workaround. But I can't say if this still works with more recent versions of Electon.

from node-auto-launch.

Connum avatar Connum commented on July 20, 2024

Here's a workaround I'm using: After initialising the AutoLauncher, overwrite the appPath with something like this:

AutoLauncher.opts.appPath += '" --autostart "'

Notice the double quotes inside the single quotes. auto-launch automatically encloses the appPath with double quotes when writing to the registry, so what this does is closing the double quotes behind the path, adding an --autostart command line argument (or whatever you want to name it) and then opening double quotes again, so you end up with this in the registry:

"X:\Your\app\path\app.exe" --autostart ""

You end up having a set of double quotes as an additional command line argument as well, but that shouldn't hurt. Now you can check in your app if the argument is set and react to it accordingly.

I only tested this under Windows and it's a bit hack-ish, but as long as adding command line arguments is not natively supported by this package, it works.

from node-auto-launch.

adam-lynch avatar adam-lynch commented on July 20, 2024

Please see that we're looking for contributors / maintainers: #64.

I'm happy to give access to people who are willing to help improve things, merge pull-requests, close issues, etc.

from node-auto-launch.

Noitidart avatar Noitidart commented on July 20, 2024

@Connum can you share more code please. I don't understand what you mean by after initializing the autolauncher.

from node-auto-launch.

Connum avatar Connum commented on July 20, 2024

@Connum can you share more code please. I don't understand what you mean by after initializing the autolauncher.

// initialize AutoLaunch
var AutoLauncher = new AutoLaunch({
    name: 'MyAppName'
});

// append argument to app path
AutoLauncher.opts.appPath += '" --autostart "';

// You can also override the app name if you like
// AutoLauncher.opts.appName = 'MyAppNameAlternative';

// enable AutoLaunch
AutoLauncher.enable();

from node-auto-launch.

Noitidart avatar Noitidart commented on July 20, 2024

Thank you! I will test on mac and let you know how it goes.

from node-auto-launch.

Noitidart avatar Noitidart commented on July 20, 2024

There seems to be an option to enable called isHiddenOnLaunch:

So if we set this to true we can detect if it was auto-launched by looking for the "hidden" argument. I haven't tried this yet, just peeked at the source and am theorizing.

from node-auto-launch.

newCodeRunner avatar newCodeRunner commented on July 20, 2024

@Noitidart , @Connum , did you find a solution for this? I am on Mac, and I can't use the solution proposed by @Connum , also the --hidden argument is not present in Electron's main entry file (process.argv)

from node-auto-launch.

newCodeRunner avatar newCodeRunner commented on July 20, 2024

@Noitidart , @Connum thank you for your quick reply.

Well for anyone looking for a solution, here is what I did.
Electron-v10.1.5
OS- Mac (Tested)
OS- Win (Should Work as proposed below)
OS- Linux (Not supported in Electron documentation)

I used the Electron's built in
app.setLoginItemSettings({ openAtLogin: true, args: ['--startup'])
and
app.getLoginItemSettings().wasOpenedAtLogin // for mac only
to check if the app was opened at startup, and then used my Electron Main process entry file to open main window or use NodeJs child-proc to execute another binary. Hacky, but this is all I got after 2 days searching.

For windows users, they can make use of NodeJS built-in (process.argv) to check if '--startup' is present at Electron Main process entry file, and then do the same processes accordingly.

from node-auto-launch.

Noitidart avatar Noitidart commented on July 20, 2024

from node-auto-launch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.