2
0
mirror of https://github.com/webgefrickel/dotfiles synced 2024-11-09 13:10:27 +00:00
steffen-dotfiles/scripts/out/index.js
Steffen Rademacker bc44ad879e major stow rewrite
2024-07-11 12:09:38 +02:00

51 lines
1.2 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
import util from 'util';
import { exec } from 'child_process';
import inquirer from 'inquirer';
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(' 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) {
inquirer.prompt([{
type: 'list',
name: 'device',
message: 'Choose your desired output audio device:',
choices: devices.map(d => d.name),
}]).then(answer => {
changeDevice(answer.device);
});
}
}
main();