Skip to content

Instantly share code, notes, and snippets.

@uchcode
Created August 9, 2017 11:29
Show Gist options
  • Save uchcode/a699865f3d1697461b17c4591714efe0 to your computer and use it in GitHub Desktop.
Save uchcode/a699865f3d1697461b17c4591714efe0 to your computer and use it in GitHub Desktop.
electron-asar-launcher.js
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