javascriptmacoselectronappleevents

electronJS / OSX - open file with my application, how to use the Apple event?


My application is an Electron.js app

I type this command in the terminal to open a file with my app:

open "/Users/Bob/Pictures/test.jpg" -a myApp

In my process.argv I get an Apple event, something like '-psn_0_#######', How do I use this event to get the jpg?


Solution

  • On windows you parse process.argv (in the main process) to get the filepath. but on mac you need to listen to the 'open-file' event. This can be done like this:

    app.on('will-finish-launching', () => {
        app.on('open-file', (event, path) => {
            // do something
        });
    });
    

    https://github.com/electron/electron/blob/master/docs/api/app.md#event-open-file-macos