How to use auto-launch to start app on system startup?
Asked Answered
H

3

15

package.json:

{
  "name": "electronapp",
  "version": "1.0.0",
  "description": "electron auto-launch",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "build": "electron-packager . --all"
  },
  "author": "ivie",
  "license": "ISC",
  "devDependencies": {
    "Q": "^1.0.0",
    "asar": "^0.13.0",
    "electron": "^1.7.6",
    "electron-packager": "^9.1.0",
    "electron-prebuilt": "^1.4.13",
    "fs-jetpack": "^1.2.0",
    "grunt-electron-installer": "^2.1.0",
    "rcedit": "^0.9.0"
  },
  "dependencies": {
    "auto-launch": "^5.0.1"
  }
}

index.js:

var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var path = require('path');

app.on('ready', ()=>{
    var mainwindow = new BrowserWindow({
        width: 1200,
        height: 800,
        icon: "favicon.ico",
        frame:true,
        title:'Menuboard',
        fullscreen: false,
        autoHideMenuBar: false
    })
    mainwindow.openDevTools();
    mainwindow.loadURL('https://www.google.com');
    mainwindow.on('closed', function() {
        mainwindow = null;
    });
});
app.on('window-all-closed', function() {
    if(process.platform != 'darwin')
        app.quit();
})

I have generated an electron .exe using this code. It's getting executed when I'm double clicking on it. But, I want to run it on windows startup. I got to know about auto-launch. But, I'm not sure how to use it in my application? Any help would be appreciated.

Hymettus answered 20/9, 2017 at 9:25 Comment(0)
M
27

Load auto-launch module:

const AutoLaunch = require('auto-launch');

Then add this after app.on('ready', ()=>{:

  let autoLaunch = new AutoLaunch({
    name: 'Your app name goes here',
    path: app.getPath('exe'),
  });
  autoLaunch.isEnabled().then((isEnabled) => {
    if (!isEnabled) autoLaunch.enable();
  });
Mum answered 29/1, 2018 at 13:49 Comment(2)
Does this starts the app automatically post installations as well?Pyrogallol
Will it work even if user is not logged in?Voltameter
P
23

FYI this is now provided by Electron out of the box:

https://electronjs.org/docs/api/app#appsetloginitemsettingssettings-macos-windows

Example:

const electron = require("electron")

electron.app.setLoginItemSettings({
    openAtLogin: arg.settings.startOnStartup,
    path: electron.app.getPath("exe")
});

EDIT

Based on new comments, this may be out of date. Consider trying Timur Nugmanov's answer first.

Poulard answered 6/10, 2018 at 8:12 Comment(5)
Does this work on all platforms, or does it need to be modified at all or linux and mac?Fistic
I only used windows for my app so I couldn't tell you.Poulard
This did not work for me, because it was complaining about the args. The answer provided just bellow this answer worked fine.Shabbir
This works fine for me on macOS (Electron 11.2.3)Answerable
+1 that it works fine for both Mac and Windows. I ran into some issues getting it to work on Windows initially because I didn't set the Application User Model ID and it ran into some conflicts in the registry app.setAppUserModelId. If you run into issues with windows, you can debug it by looking into HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run registry.Brit
E
14

At current electron release(19.0.0), the code below works fine:

app.setLoginItemSettings({
    openAtLogin: true    
})
Eyeshot answered 3/6, 2022 at 3:35 Comment(3)
Easiest solution as of March 2023.Lunitidal
According to the docs, this only works on Windows and Mac...Watts
As far as I know, it only works on Mac and Windows and don't support linux.Horripilation

© 2022 - 2024 — McMap. All rights reserved.