Custom node-js script for audio output device switching

main
Steffen Rademacker 2 years ago
parent c98a927966
commit 7bda2f1973

@ -41,3 +41,7 @@ pip install tt-time-tracker
# clipper
mkdir -p ~/Library/LaunchAgents
brew services start clipper
# switchaudio + custom node-script
cd ~/dotfiles/other/out
npm install

@ -0,0 +1,55 @@
import util from 'util';
import { exec } from 'child_process';
import enquirer from 'enquirer';
const devices = [];
const asyncExec = util.promisify(exec);
const parseJson = jsonString => {
try {
return JSON.parse(jsonString);
} catch (e) {
return undefined;
}
};
async function changeDevice(device) {
const names = devices.map(d => d.name);
const ids = devices.map(d => d.id);
const index = names.findIndex(element => element === device);
const id = ids[index];
await asyncExec(`SwitchAudioSource -i ${id}`);
console.log(` Selected ${device} as new audio output device`);
console.log(' Restarting Spotify Daemon');
await asyncExec('brew services restart spotifyd');
console.log(' Done 😀🎧🤘');
}
async function main() {
const { stdout } = await asyncExec('SwitchAudioSource -a -f json');
const lines = stdout.split(/\r?\n/).filter(element => element);
lines.forEach(line => {
const device = parseJson(line);
if (device !== undefined && device.type === 'output') {
devices.push(device);
}
});
if (devices.length > 0) {
const { AutoComplete } = enquirer;
const prompt = new AutoComplete({
name: 'device',
multiple: false,
message: 'Pick output audio device',
limit: 10,
initial: 0,
choices: devices.map(d => d.name),
});
prompt.run().then(device => changeDevice(device));
}
}
main();

@ -0,0 +1,50 @@
{
"name": "out",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "out",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"enquirer": "^2.3.6"
}
},
"node_modules/ansi-colors": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz",
"integrity": "sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==",
"engines": {
"node": ">=6"
}
},
"node_modules/enquirer": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz",
"integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==",
"dependencies": {
"ansi-colors": "^4.1.1"
},
"engines": {
"node": ">=8.6"
}
}
},
"dependencies": {
"ansi-colors": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz",
"integrity": "sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw=="
},
"enquirer": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz",
"integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==",
"requires": {
"ansi-colors": "^4.1.1"
}
}
}
}

@ -0,0 +1,15 @@
{
"name": "out",
"version": "1.0.0",
"description": "Audio Output Switcher",
"main": "main.js",
"type": "module",
"scripts": {
"run": "node main.js"
},
"author": "Steffen Rademacker <kontakt@webgefrickel.de>",
"license": "MIT",
"dependencies": {
"enquirer": "^2.3.6"
}
}

@ -17,6 +17,7 @@ alias mutt='neomutt'
alias nb="npm run build"
alias npmre='rm -f package-lock.json && rm -rf node_modules && npm install'
alias ns="npm start"
alias out="node ~/Dotfiles/other/out/main.js"
alias q='exit'
alias sudo='sudo '
alias t='tmux -u -2'

@ -53,11 +53,6 @@ wttr () {
fi
}
out () {
SwitchAudioSource -n
brew services restart spotifyd
}
webvideo () {
if [ $# -eq 0 ]; then
echo "No arguments supplied, example usage: webvideo FILENAME --resize 1920 --mute"

Loading…
Cancel
Save