Open deep-bound application and pass options using Electron (macOS)

I want to open deep-bound application and pass parameters using Electron (macOS).

The electronic deep link-mac-win project is on GitHub .

enter image description here

Edited package.json , after the 'quick-setup-guide-electronics developer-builder to create a mac installer:

 { "name": "electron-deep-linking-osx", "version": "1.0.0", "description": "A minimal Electron application with Deep Linking (OSX)", "main": "main.js", "scripts": { "start": "electron .", "pack": "build --dir", "dist": "build" }, "repository": "https://github.com/oikonomopo/electron-deep-linking-osx", "keywords": [ "Electron", "osx", "deep-linking" ], "author": "GitHub", "license": "CC0-1.0", "devDependencies": { "electron": "1.6.6", "electron-builder": "17.1.2" }, "build": { "appId": "your.id", "mac": { "category": "your.app.category.type" }, "protocols": { "name": "myApp", "schemes": ["myApp"] } } } 

Edited main.js , added code to register myapp URL protocol protocol , listen for 'open-url' events and write down the arguments:

 const electron = require('electron') // Module to control application life. const app = electron.app // Module to create native browser window. const BrowserWindow = electron.BrowserWindow // Module with utilities for working with file and directory paths. const path = require('path') // Module with utilities for URL resolution and parsing. const url = require('url') // Module to display native system dialogs for opening and saving files, alerting, etc. const dialog = electron.dialog // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow function createWindow () { // Create the browser window. mainWindow = new BrowserWindow({width: 800, height: 600}) // and load the index.html of the app. mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // 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 }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', function () { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { // On OS X it common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (mainWindow === null) { createWindow() } }) // In this file you can include the rest of your app specific main process // code. You can also put them in separate files and require them here. // The setAsDefaultProtocolClient only works on packaged versions of the application app.setAsDefaultProtocolClient('myApp') // Protocol handler for osx app.on('open-url', function (event, url) { event.preventDefault(); log("open-url event: " + url) dialog.showErrorBox('open-url', `You arrived from: ${url}`) }) // Log both at terminal and at browser function log(s) { console.log(s) if (mainWindow && mainWindow.webContents) { mainWindow.webContents.executeJavaScript(`console.log("${s}")`) } } 

Steps to life :-)

 # Clone this repository git clone https://github.com/oikonomopo/electron-deep-linking-mac-win.git # Go into the repository cd electron-deep-linking-mac-win # Install dependencies npm install # Run the app npm start # Produce installer npm run dist 

After starting the installer ( electron-deep-linking-mac-win/dist/electron-quick-start-1.0.0.dmg ), I will try to open the deep-binding os electronic deep communication application by entering myapp://param in the address bar Safari

  • If the application is open, it activates, and I see the dialog box and the open-url event: myapp://param !

  • If the application is closed, it opens, the dialog is displayed with the correct URL, but it is not written to the dev console!

Why is the url displayed correctly in the dialog module but not registered in the dev console? How to register it?

Looking for a solution using only electron-builder ( which uses electron-packager )

+6
electron macos electron-builder
source share
2 answers

Solved for macOS and win32 (Updated project 'electronic deep link-mac-win' on GitHub ).


package.json :

 { "name": "electron-deeplinking-macos-win32", "version": "0.0.1", "description": "Minimal Electron application with deep inking (macOS/win32)", "main": "main.js", "scripts": { "start": "electron .", "pack": "build --dir", "dist": "build" }, "repository": "https://github.com/oikonomopo/electron-deep-linking-osx", "author": "oikonomopo", "license": "CC0-1.0", "devDependencies": { "electron": "1.6.6", "electron-builder": "17.1.2" }, "build": { "appId": "oikonomopo.electron-deeplinking-macos-win32", "protocols": { "name": "electron-deep-linking", "schemes": ["myapp"] }, "mac": { "category": "public.app-category.Reference" }, "win": { } } } 

main.js :

 const {app, BrowserWindow} = require('electron') // Module with utilities for working with file and directory paths. const path = require('path') // Module with utilities for URL resolution and parsing. const url = require('url') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow // Deep linked url let deeplinkingUrl // Force Single Instance Application const shouldQuit = app.makeSingleInstance((argv, workingDirectory) => { // Someone tried to run a second instance, we should focus our window. // Protocol handler for win32 // argv: An array of the second instance's (command line / deep linked) arguments if (process.platform == 'win32') { // Keep only command line / deep linked arguments deeplinkingUrl = argv.slice(1) } logEverywhere("app.makeSingleInstance# " + deeplinkingUrl) if (win) { if (win.isMinimized()) win.restore() win.focus() } }) if (shouldQuit) { app.quit() return } function createWindow () { // Create the browser window. mainWindow = new BrowserWindow({width: 800, height: 600}) // and load the index.html of the app. mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // Open the DevTools. mainWindow.webContents.openDevTools() // Protocol handler for win32 if (process.platform == 'win32') { // Keep only command line / deep linked arguments deeplinkingUrl = process.argv.slice(1) } logEverywhere("createWindow# " + deeplinkingUrl) // 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 }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', function () { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { // On OS X it common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (mainWindow === null) { createWindow() } }) // Define custom protocol handler. Deep linking works on packaged versions of the application! app.setAsDefaultProtocolClient('myapp') // Protocol handler for osx app.on('open-url', function (event, url) { event.preventDefault() deeplinkingUrl = url logEverywhere("open-url# " + deeplinkingUrl) }) // Log both at dev console and at running node console instance function logEverywhere(s) { console.log(s) if (mainWindow && mainWindow.webContents) { mainWindow.webContents.executeJavaScript(`console.log("${s}")`) } } 

main.js code description:

  • In let deeplinkingUrl we save the provided url.

  • On macOS, you need to listen for the app.open-url event, while on Windows the URL should be available in process.argv (in the main process).

    • On the macOS platform, this is fixed in the 'open-url' event , we set it using deeplinkingUrl = url ! (See // Protocol handler for osx )

    • On the win32 platform, this is stored in process.argv along with other arguments. To get only the provided url, deeplinkingUrl = argv.slice(1) . (See // Protocol handler for win32 )

    • In the app.makeSingleInstance method, we check which platform we are on, and we set deeplinkingUrl accordingly! If we are on the win32 platform, the url is in the argv variable from the callback, otherwise the 'open-url' event should already be set on macOS! (See // Force Single Instance Application )

+6
source share

You must configure the open-url event in the will-finish-launching according to the docs . I had the same weird behavior with open-file until it was configured in the will-finish-launching .

You notice that they did this in the example that you are referring to .

Although he mentions this in the will-finish-launching , he should really mention this in the open-url and open-file docs, as it is pretty easy to skip.

 app.on('will-finish-launching', () => { // Protocol handler for osx app.on('open-url', (event, url) => { event.preventDefault(); log("open-url event: " + url) }) }); 
+2
source share

All Articles