bopen
Version:
A better native open utility. Open URLs, paths and locators in the default or specified browser with options including opening in incognito mode. Cross-platform.
65 lines (53 loc) • 1.63 kB
JavaScript
const DefaultBrowser = require('x-default-browser')
const applescript = require('applescript')
exports.getDefaultBrowser = () => {
return new Promise((resolve, reject) => {
DefaultBrowser((err, browser) => {
if (err) {
return reject(new Error(err))
}
return resolve(browser.commonName)
})
})
}
exports.activateApp = (name) => {
if (process.platform === 'darwin') {
setTimeout(() => {
applescript.execString(`activate application "${name}"`)
}, 500)
}
}
exports.openSafariIncognito = (url) => {
return new Promise((resolve, reject) => {
if (process.platform !== 'darwin') {
return reject(new Error('Unsupported platform. macOS only'))
}
const script = `
on isRunning(appName)
tell application "System Events" to (name of processes) contains appName
end isRunning
set isSafariRunning to isRunning("Safari")
if isSafariRunning then
tell application "Safari" to activate
delay 0.5
tell application "System Events" to keystroke "n" using {command down, shift down}
else
tell application "Safari" to activate
delay 0.5
tell application "System Events"
keystroke "w" using command down
keystroke "n" using {command down, shift down}
end tell
end if
delay 0.5
tell application "Safari" to set the URL of the front document to "${url}"
`
applescript.execString(script, (err, response) => {
if (err) {
return reject(new Error(err))
}
return resolve(response)
})
})
}