From 8e8b3948dabc90b7ca597c1c6617e8e1a0ce20ad Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Wed, 31 May 2017 23:08:57 +0530 Subject: [PATCH] Electron-26 (Auto Launch for Mac) (#109) * Electron-19 - Updated AutoLauncher path to make it work properly in mac * Electron-26 - Implemented auto launch for mac * Electron-26 - Added comments --- js/main.js | 4 ++-- js/menus/menuTemplate.js | 44 +++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/js/main.js b/js/main.js index f1b2d8fd1..80d5bb037 100644 --- a/js/main.js +++ b/js/main.js @@ -7,7 +7,7 @@ const squirrelStartup = require('electron-squirrel-startup'); const AutoLaunch = require('auto-launch'); const urlParser = require('url'); const { getConfigField } = require('./config.js'); -const { isDevEnv} = require('./utils/misc.js'); +const { isMac, isDevEnv } = require('./utils/misc.js'); const protocolHandler = require('./protocolHandler'); // used to check if a url was opened when the app was already open @@ -93,7 +93,7 @@ function setupThenOpenMainWindow() { process.argv.some((val) => { let flag = '--install'; - if (val === flag) { + if (val === flag && !isMac) { installMode = true; getConfigField('launchOnStartup') .then(setStartup) diff --git a/js/menus/menuTemplate.js b/js/menus/menuTemplate.js index 2d8b1109e..e6f331bd4 100644 --- a/js/menus/menuTemplate.js +++ b/js/menus/menuTemplate.js @@ -4,6 +4,7 @@ const electron = require('electron'); const { getConfigField, updateConfigField } = require('../config.js'); const AutoLaunch = require('auto-launch'); const isMac = require('../utils/misc.js').isMac; +const childProcess = require('child_process'); var minimizeOnClose = false; var launchOnStartup = false; @@ -14,6 +15,7 @@ var symphonyAutoLauncher = new AutoLaunch({ name: 'Symphony', path: process.execPath, }); +let launchAgentPath = '~/Library/LaunchAgents/com.symphony.symphony-desktop.agent.plist'; const template = [ { @@ -184,17 +186,39 @@ function getTemplate(app) { checked: launchOnStartup, click: function (item) { if (item.checked){ - symphonyAutoLauncher.enable() - .catch(function (err) { - let title = 'Error setting AutoLaunch configuration'; - electron.dialog.showErrorBox(title, title + ': ' + err); - }); + if (isMac){ + // TODO: Need to change this implementation to AutoLaunch once they fix this issue -> + // https://github.com/Teamwork/node-auto-launch/issues/28 + childProcess.exec(`launchctl load ${launchAgentPath}`, (err) => { + if (err){ + let title = 'Error setting AutoLaunch configuration'; + electron.dialog.showErrorBox(title, 'Please try reinstalling the application'); + } + }); + } else { + symphonyAutoLauncher.enable() + .catch(function (err) { + let title = 'Error setting AutoLaunch configuration'; + electron.dialog.showErrorBox(title, title + ': ' + err); + }); + } } else { - symphonyAutoLauncher.disable() - .catch(function (err) { - let title = 'Error setting AutoLaunch configuration'; - electron.dialog.showErrorBox(title, title + ': ' + err); - }); + if (isMac){ + // TODO: Need to change this implementation to AutoLaunch once they fix this issue -> + // https://github.com/Teamwork/node-auto-launch/issues/28 + childProcess.exec(`launchctl unload ${launchAgentPath}`, (err) => { + if (err){ + let title = 'Error disabling AutoLaunch configuration'; + electron.dialog.showErrorBox(title, 'Please try reinstalling the application'); + } + }); + } else { + symphonyAutoLauncher.disable() + .catch(function (err) { + let title = 'Error setting AutoLaunch configuration'; + electron.dialog.showErrorBox(title, title + ': ' + err); + }); + } } launchOnStartup = item.checked; updateConfigField('launchOnStartup', launchOnStartup);