Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if app launched as hidden at login or manually by the user #93

Closed
ilDon opened this issue Feb 1, 2019 · 13 comments
Closed

Check if app launched as hidden at login or manually by the user #93

ilDon opened this issue Feb 1, 2019 · 13 comments

Comments

@ilDon
Copy link

ilDon commented Feb 1, 2019

I have integrated auto-launch in my Electron app. I would like not to open the main window of the app when the app is auto-launched at startup. Is there a way to determine if the app was launched by the user, or if it has been auto-launched at system startup?

@Connum
Copy link

Connum commented Mar 4, 2019

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.

@adam-lynch
Copy link
Contributor

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.

@Noitidart
Copy link

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

@Connum
Copy link

Connum commented May 2, 2019

@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();

@Noitidart
Copy link

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

@Noitidart
Copy link

Noitidart commented Jun 10, 2019

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.

@ghost
Copy link

ghost commented Oct 30, 2020

@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)

@Noitidart
Copy link

I didn't find a solution. :(

@Connum
Copy link

Connum commented Oct 30, 2020

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.

@ghost
Copy link

ghost commented Oct 31, 2020

@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.

@Noitidart
Copy link

Noitidart commented Oct 31, 2020 via email

@stenya
Copy link

stenya commented Mar 11, 2021

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();
}

@Oxalin
Copy link
Collaborator

Oxalin commented Apr 10, 2024

This is the way to go, at least for now.

Auto-launch could have dealt with extra arguments uniformly without this specific use case. Since we handle this one specifically ("hidden"), maybe we could explore what could be improved in auto-launch to make things even easier.

I'm opening an issue to track some ideas: #128

@Oxalin Oxalin changed the title Check if electron app is launched by user or by system at OS startup Check if app is hidden at launch at login or manually by the user Apr 10, 2024
@Oxalin Oxalin closed this as completed Apr 10, 2024
@Oxalin Oxalin changed the title Check if app is hidden at launch at login or manually by the user Check if app launched as hidden at login or manually by the user Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants