Created
August 9, 2017 11:29
-
-
Save uchcode/a699865f3d1697461b17c4591714efe0 to your computer and use it in GitHub Desktop.
electron-asar-launcher.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = Application.currentApplication() | |
app.includeStandardAdditions = true | |
se = Application('System Events') | |
function sh(script, opt={}) { | |
return app.doShellScript(script, { | |
administratorPrivileges: !!opt.withPrompt, | |
withPrompt: opt.withPrompt || '', | |
alteringLineEndings: opt.alteringLineEndings || false | |
}).replace(/\n$/,'') | |
} | |
function openElectronWithoutArguments() { | |
let name = 'Electron.app/Contents/MacOS/Electron' | |
let se = Application('System Events') | |
try { | |
var pid = sh(`ps aux`).split('\n').filter(e=>e.endsWith(name))[0].replace(/\s+/g,' ').split(' ')[1] | |
} catch(e) { | |
var pid = undefined | |
} | |
if (!pid) { | |
sh('open -n -a Electron') | |
return | |
} | |
let el = se.processes.whose({unixId:pid}) | |
if (!el) { | |
sh('open -n -a Electron') | |
return | |
} | |
el[0].frontmost = true | |
} | |
function openElectron(path) { | |
try { | |
var pid = sh(`ps aux`).split('\n').filter(e=>e.endsWith(path))[0].replace(/\s+/g,' ').split(' ')[1] | |
} catch(e) { | |
var pid = undefined | |
} | |
if (!pid) { | |
sh(`open -n -a Electron --args "${path}"`) | |
return | |
} | |
let el = se.processes.whose({unixId:pid}) | |
if (!el) { | |
sh(`open -n -a Electron --args "${path}"`) | |
return | |
} | |
el[0].frontmost = true | |
} | |
function run(input, parameters) { | |
if (input.length > 0) { | |
for (let i of input) { | |
openElectron(i.toString()) | |
} | |
} else { | |
openElectronWithoutArguments() | |
} | |
return input | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment