Skip to content

Instantly share code, notes, and snippets.

@thalesmello
Created November 22, 2018 09:07
Show Gist options
  • Save thalesmello/a6e409b2df21d9dfe07420840ce67b39 to your computer and use it in GitHub Desktop.
Save thalesmello/a6e409b2df21d9dfe07420840ce67b39 to your computer and use it in GitHub Desktop.
Close distractions using JavaScript for Automation (JXA) for Mac OS
var app = Application.currentApplication()
app.strictParameterType = true
var sitesBlacklist = [
/reddit.com$/,
/youtube.com$/,
]
var appsBlacklist = [
"Slack",
"WhatsApp",
]
function run() {
closeChromeTabs()
closeApps()
doNotDisturb()
}
function doNotDisturb() {
const system = Application("System Events")
const systemUi = system.processes["SystemUIServer"]
try {
const notificationCenterIcon = systemUi.menuBars[0].menuBarItems["Notification Center"]
notificationCenterIcon.click()
const notificationsSlider = system.processes["NotificationCenter"].uiElements[0].uiElements[1]
const doNotDisturbBox = notificationsSlider.uiElements[0].uiElements[0].uiElements[0].uiElements[2].click()
notificationCenterIcon.click()
} catch (err) {
if (err.message.includes("Can't get object")) {
return
} else {
throw err
}
}
}
function closeApps() {
const system = Application("System Events")
const apps = toArray(system.processes)
const appsToQuit = []
for (const app of apps) {
const name = app.name()
console.log(name)
for (const blocked of appsBlacklist) {
if (blocked === name) {
appsToQuit.push(name)
}
}
}
for (const app of appsToQuit) {
const appProcess = Application(app)
appProcess.quit()
}
}
function closeChromeTabs() {
const chrome = Application("Google Chrome")
const tabs = toArray(chrome.windows).flatMap(w => toArray(w.tabs))
for (const tab of tabs) {
const host = parseUrl(tab.url()).host
for (const blocked of sitesBlacklist) {
if (blocked.test(host)) {
tab.close()
}
}
}
}
function toArray(array) {
return Array.prototype.slice.call(array)
}
function parseUrl (str, component) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/parse_url/
// original by: Steven Levithan (http://blog.stevenlevithan.com)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// input by: Lorenzo Pisani
// input by: Tony
// improved by: Brett Zamir (http://brett-zamir.me)
// note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
// note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: Does not replace invalid characters with '_' as in PHP,
// note 1: nor does it return false with
// note 1: a seriously malformed URL.
// note 1: Besides function name, is essentially the same as parseUri as
// note 1: well as our allowing
// note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
// example 1: parse_url('http://user:pass@host/path?a=v#a')
// returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
// example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
// returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
// example 3: parse_url('https://host.domain.tld/[email protected]/folder')
// returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/[email protected]/folder'}
// example 4: parse_url('https://gooduser:[email protected]/[email protected]/folder?foo=bar')
// returns 4: { scheme: 'https', host: 'www.example.com', path: '/[email protected]/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
var query
var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
var key = [
'source',
'scheme',
'authority',
'userInfo',
'user',
'pass',
'host',
'port',
'relative',
'path',
'directory',
'file',
'query',
'fragment'
]
// For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
var parser = {
php: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'()',
'(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
strict: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
loose: new RegExp([
'(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
'(?:\\/\\/\\/?)?',
'((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
'(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
'(?:\\?([^#]*))?(?:#(.*))?)'
].join(''))
}
var m = parser[mode].exec(str)
var uri = {}
var i = 14
while (i--) {
if (m[i]) {
uri[key[i]] = m[i]
}
}
if (component) {
return uri[component.replace('PHP_URL_', '').toLowerCase()]
}
if (mode !== 'php') {
var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
parser = /(?:^|&)([^&=]*)=?([^&]*)/g
uri[name] = {}
query = uri[key[12]] || ''
query.replace(parser, function ($0, $1, $2) {
if ($1) {
uri[name][$1] = $2
}
})
}
delete uri.source
return uri
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment