1

Currently I need to get command line arguments from an Electron application. So if you start the application with command line arguments, I need to use the arguments within the renderer process (a webview to be specific.)

This works fine when opening the application for the first time (using onload), but I'm having issues getting this to work when the application is already running and the user tries to reopen the app from cmd with args.

Currently, I'm using:

let closeDupeApp = app.makeSingleInstance(function(commandLine, workingDirectory) {
    // Someone tried to run a second instance, we should focus our window.
    if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
    }
});

To prevent a second window from opening. However, I need the commandLine arguments and I need to send them to the renderer process.

Is there a way to do this?

2
  • Did you do a search on this? Anyway you can get command line arguments from the global variable process. The property name is argv. You can read process.argv anytime to get the relevant data. Commented Jan 2, 2018 at 19:55
  • Process.argv doesn't help me. I need to send the arguments to the renderer process upon attempting to launch the application. The provided method is the only way I have found to detect such an occurrence. I need a way to send commandLine to a renderer function upon it happening. Commented Jan 2, 2018 at 20:04

2 Answers 2

3

You can read process.argv in the main process and paste it onto the end of the file:// url as a query string in BrowserWindow.open(). I do this routinely. You can JSON encode the and encodeURIComponent on the array to get a nice list in the renderer process.

Sign up to request clarification or add additional context in comments.

Comments

3

Found a solution:

main.js

let closeDupeApp = app.makeSingleInstance(function(commandLine, workingDirectory) {
    if (mainWindow) {
        // Send args to renderer
        mainWindow.webContents.send("cmdArgs", commandLine);

        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
    }
});

rendererFile.js

require('electron').ipcRenderer.on('cmdArgs', function(event, cmdArgs) {
    // STUFF
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.