Assuming that you have set a Keyboard Maestro variable called soundDevice to the name of your preferred device (or some distinct part of its name that appears in the System Preferences sound section), then I think a quick patch might look like this:
(For an Execute JavaScript for Automation action, or for prior testing in Script Editor, with the Language tab at top left set to JavaScript rather than AppleScript
(function (strDevice) {
'use strict';
var e = Application("System Preferences"),
b = !1,
d;
e.panes["com.apple.preference.sound"].anchors.output.reveal();
var a = Application("System Events")
.processes["System Preferences"].windows[
0].tabGroups[0].scrollAreas[0].tables[0].rows,
c = a.where({
selected: !0
}),
c = c.length ? c[0].textFields[0].value() : null;
if (!c) return null;
var f = strDevice;
a()
.forEach(function (a) {
b || (d = a.textFields[0].value(), -1 !== d.indexOf(f) &&
(a.select(), b = !0));
});
a = Application.currentApplication();
a.includeStandardAdditions = !0;
a.activate();
a.displayNotification(b ? "-> " + d :
"Couldn't find a sound device containing this string: \"" + f + '"', {
withTitle: "Sound Output Device " + (b ? "" : "NOT ") + "Changed"
});
e.quit();
return b;
})(Application("Keyboard Maestro Engine")
.getvariable(
'soundDevice'
)
);
I was looking for a way to cycle through audio devices and found many topics in KM forums. After testing some of the solutions, I chose the executable file solution AudioSwitcher.zip (107.1 KB) from @ccstone's reply.
Drag the executable file and drop it onto Applications folder, unless you want to keep it somewhere else then you would also need to change the default path of the executable file in the AppleScript below from "/Applications/AudioSwitcher" to your favorite path.
I made a few changes to this AppleScript StackExchange - SwitchAudioSource. The dialogue box seems unnecessary in my opinion so I deleted its line. I replaced it with a notification to know which output device is the current one. Also replaced " -c" with "-n".
on run
set theSwitch to "/Applications/AudioSwitcher"
do shell script theSwitch & "-n"
end run
These tips helped me a lot, as well. Thank you all! Not sure if this justifies opening up a new thread, so I'll try a reply here first:
While the macros discussed here change the audio output source, I would like to have something similar changing the input. So, whether I want to use the internal Mac microphone or the one attached to my USB headset. Ideally of course, this is combined with the output - so when I switch output to the headset, the input/mic should switch as well and vice versa.
Since you use the external shell script for the audio output, I guess I need something like this for the input as well?
Perhaps it helps someone, so I described how I got it to work:
I've tried the instructions from Jun 2016 from @ccstone
But I was always told AudioSwitcher not found.
There is no list generated by the -c switch – that only displays the current device.
Run:
SwitchAudioSource -h
In the Terminal.app to see SwitchAudioSource's help.
To list all available devices:
SwitchAudioSource -a
To discover if a certain device exists in Bash:
deviceExists=$(SwitchAudioSource -a | awk '/Soundflower \(2ch\) \(output\)/ { print }')
if [ "$deviceExists" != '' ]; then
echo "Device $deviceExists Exists!"
else
echo "Device $deviceExists Does NOT Exist!"
fi
You could also do this by:
Running SwitchAudioSource -a and capturing the output to a variable.
Then use Keyboard Maestro's own search to look for your device.
My goal is the following:
In my office I want to switch between A/B.
At home I want to switch between A/C
(B always exists because it's the internal speaker, C is only connected at home)
Here is the script, that makes sense to me (but doesn't work):
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
currentOutput=$(SwitchAudioSource -c); echo $currentOutput
deviceExists=$(SwitchAudioSource -a | awk '/C \(output\)/ { print }')
if [ "$currentOutput" = 'C' || "$currentOutput" = 'B' ]; then
SwitchAudioSource -s 'A'
else
if [ "$deviceExists" != '' ]; then
SwitchAudioSource -s 'C'
else
SwitchAudioSource -s 'B'
fi