如何在系统启动时使用自动启动来启动应用程序?

问题描述 投票:1回答:2

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

我使用此代码生成了一个电子.exe。当我双击它时它会被执行。但是,我想在Windows启动时运行它。我了解了自动启动。但是,我不确定如何在我的应用程序中使用它?任何帮助,将不胜感激。

javascript node.js node-modules electron-builder electron-packager
2个回答
5
投票

仅供参考,现在由Electron开箱提供:

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

例:

const electron = require("electron")

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

3
投票

加载自动启动模块:

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

然后在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();
  });
© www.soinside.com 2019 - 2024. All rights reserved.