Skip to content

Instantly share code, notes, and snippets.

@LoganDark
Last active December 23, 2019 10:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LoganDark/704b15a798ca72bb3732844e8e440e3d to your computer and use it in GitHub Desktop.
Save LoganDark/704b15a798ca72bb3732844e8e440e3d to your computer and use it in GitHub Desktop.
A notifier that warns you when macOS is about to crash.
// This is JXA, i.e. you can't run it with plain `node`. Use `osascript` or the Script Editor.
// I recommend exporting it to an `.app` and running it at login if you have my same issue.
// Requires this script: https://gist.github.com/LoganDark/b8c74e14c06b77e6f16d4492c5bda928
// CONFIGURE crashPercentScript in run() to point to your script. Make sure to chmod +x to make
// it executable.
function getCrashPercent(app, script) {
return +/\d+/.exec(app.doShellScript(script).split('\r')[0])[0]
}
function run() {
const crashPercentScript = '/Users/LoganDark/.bin/crash-percent'
const app = Application.currentApplication()
app.includeStandardAdditions = true
console.log('Displaying startup dialog')
app.activate()
const result = app.displayDialog('Corruption notifier has been started.\r\rYou will be notified of your system corruption every 5 minutes once corruption exceeds 90%.', {
withTitle: 'Info',
buttons: ['OK'],
defaultButton: 'OK',
timeout: Infinity
})
console.log('Waiting for corruption to exceed 90%')
while (true) {
console.log('Getting corruption percent...')
const crashPercent = getCrashPercent(app, crashPercentScript)
console.log('Corruption percent is ' + crashPercent)
if (crashPercent >= 90) {
console.log('Corruption >= 90%!')
break
} else {
console.log('Corruption is fine... for now')
delay(5)
}
}
while (true) {
console.log('Getting corruption percent...')
const crashPercent = getCrashPercent(app, crashPercentScript)
console.log('Bringing up prompt for system corruption')
app.activate()
const result = app.displayDialog('System corruption is at ' + crashPercent + '%.', {
withTitle: 'Warning',
buttons: ['Stop bugging me', 'Kill', 'OK'],
defaultButton: 'OK',
timeout: Infinity
})
if (result.buttonReturned === 'Kill') {
console.log('User chose "kill", confirming')
app.activate()
const result = app.displayDialog('Are you sure you\'d like to kill loginwindow? This will fix your system corruption, but will also log you out immediately and close all open applications.', {
withTitle: 'Log out?',
buttons: ['Yes', 'No'],
defaultButton: 'No',
timeout: Infinity
})
if (result.buttonReturned === 'Yes') {
console.log('Killing loginwindow...')
return app.doShellScript(crashPercentScript + ' --kill')
}
} else if (result.buttonReturned === 'Stop bugging me') {
console.log('Will stop bugging now')
return
}
console.log('Waiting 5 minutes')
delay(300)
}
}
@PatrikTheDev
Copy link

Hi, this app stops responding as soon as I click OK on the first notification, what should I do?

@LoganDark
Copy link
Author

@Patrik-svobodik Sorry for taking so long to respond, but it's not supposed to respond or bring anything else up until system corruption reaches >90%.

Once it does that, it will pop up a window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment