Skip to content

Instantly share code, notes, and snippets.

@LoganDark
Last active December 23, 2019 10:18

Revisions

  1. LoganDark revised this gist Sep 11, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions crash-percent.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    // 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]
  2. LoganDark created this gist Sep 11, 2018.
    88 changes: 88 additions & 0 deletions crash-percent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    // This is JXA, i.e. you can't run it with plain `node`. Use `osascript` or the Script Editor.

    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)
    }
    }