Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an electron app with arguments?

My app is electron with a BrowserWindow loading a local page index.html.
I call npm run start a script to run electron main.js , the app opens and the html loaded.
Can I add an argument to the script that will load different html file into the BrowserWindow ?

In the main.js file the code is :

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    webPreferences:{
      webSecurity:false
    },
    fullscreen : false });//, alwaysOnTop : true , kiosk : true })
  mainWindow.setMenu(null);
  // and load the index.html of the app.
  let url = `file://${__dirname}/index.html`; \\ index.html should be determined by argument passed at start.  
  mainWindow.loadURL(url,loadOptions);

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
}
like image 983
Haddar Macdasi Avatar asked Dec 18 '22 13:12

Haddar Macdasi


1 Answers

To pass the command line arguments to electron app:

./node_modules/.bin/electron main.js --arg1=value --arg2=value

It can be retrieved like this in the main.js:

import { app } from "electron";
app.commandLine.getSwitchValue("arg1");
app.commandLine.getSwitchValue("arg2");
like image 99
Ali Raza Avatar answered Dec 28 '22 10:12

Ali Raza