Created
November 30, 2024 13:29
-
-
Save Strajk/1957d186de3ec8968f93c6427f283018 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Night Shift | |
// Description: Control Night Shift on macOS | |
// Author: Pavel 'Strajk' Dolecek | |
// Acknowledgements: | |
// - https://github.com/smudge/nightlight | |
// - https://github.com/shmulvad/alfred-nightshift/ | |
// Notes: | |
// nightlight CLI usage: | |
// on/off: `nightlight on|off|toggle`, status will show current state | |
// temp: `nightlight temp [0-100]`, no argument will show current temperature | |
// schedule: `nightlight schedule [start|off|HH:MM HH:MM]` | |
import "@johnlindquist/kit" | |
import {Choice} from "@johnlindquist/kit"; | |
async function main() { | |
await execa('which', ['nightlight']) | |
.catch(async () => { | |
let choice = await arg({ | |
placeholder: `Nightlight CLI required, but not installed, install?`, | |
choices: ['Yes', 'No'] | |
}) | |
if (choice !== 'Yes') { | |
await notify(`OK, not installing Nightlight CLI & exiting`) | |
exit() // exit whole script | |
} | |
console.log(`Installing Nightlight CLI... on Apple Silicon it might take a while because precompiled binaries are not available yet, see https://github.com/smudge/nightlight/issues/22`) | |
await terminal(`brew install smudge/smudge/nightlight`) | |
await arg("Press enter when installing in terminal finishes...") | |
console.clear() // Clear previous console.log | |
return main() | |
}) | |
let status = execaSync('nightlight', ['status']).stdout | |
let temp = execaSync('nightlight', ['temp']).stdout | |
let choices: Choice[] = [{ | |
name: 'Toggle', | |
value: 'toggle' | |
}, { | |
name: 'Off', | |
value: 'off' | |
}, { | |
name: 'On', | |
value: 'on' | |
}, { | |
name: 'On & 25%', | |
value: 'temp 25' | |
}, { | |
name: 'On & 50%', | |
value: 'temp 50' | |
}, { | |
name: 'On & 75%', | |
value: 'temp 75' | |
}, { | |
name: 'On & 100%', | |
value: 'temp 100' | |
}] | |
let choice = await arg({ | |
placeholder: `Either select or type exact amount from 0 to 100`, | |
hint: `Current status: ${uppercaseFirst(status)} • Current temperature: ${temp}`, | |
choices, | |
strict: false // allow arbitrary input | |
}) | |
let args = choice.split(' ') | |
let isArg0Number = !isNaN(parseInt(args[0])) | |
if (isArg0Number) { | |
args = ['temp', args[0]] | |
} | |
if (args[0] === 'temp') { | |
// if setting temp, turn on first | |
await execa('nightlight', ['on']) | |
} | |
await execa('nightlight', args) | |
.then(() => notify(`Nightlight set to: ${choice}`)) | |
.catch(() => notify(`Failed to set Nightlight to: ${choice}`)) | |
} | |
void main() | |
function uppercaseFirst(str: string) { | |
return str.charAt(0).toUpperCase() + str.slice(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment