2023-09-30 15:37:21 +00:00
|
|
|
|
#!/usr/bin/env node
|
2022-06-16 18:41:35 +00:00
|
|
|
|
import util from 'util';
|
|
|
|
|
import { exec } from 'child_process';
|
2022-10-17 09:05:16 +00:00
|
|
|
|
import inquirer from 'inquirer';
|
2022-06-16 18:41:35 +00:00
|
|
|
|
|
|
|
|
|
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}`);
|
2022-10-17 09:05:16 +00:00
|
|
|
|
console.log(`❯ Selected »${device}« as new audio output device`);
|
2023-10-06 18:19:02 +00:00
|
|
|
|
console.log('❯ Done 🤘');
|
2022-06-16 18:41:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2022-10-17 09:05:16 +00:00
|
|
|
|
inquirer.prompt([{
|
|
|
|
|
type: 'list',
|
2022-06-16 18:41:35 +00:00
|
|
|
|
name: 'device',
|
2022-10-17 09:05:16 +00:00
|
|
|
|
message: 'Choose your desired output audio device:',
|
2022-06-16 18:41:35 +00:00
|
|
|
|
choices: devices.map(d => d.name),
|
2022-10-17 09:05:16 +00:00
|
|
|
|
}]).then(answer => {
|
|
|
|
|
changeDevice(answer.device);
|
2022-06-16 18:41:35 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|