Switch Audio Source

An additional approach, if you don't want to install SwitchAudioSource, and can run JavaScript for Applications (Yosemite onwards) would be a macro like this:

(Edit the names of the two variables to strings which distinguish the two devices which you are toggling - in my case Internal and USB – there is no need to give the full name of the device)

Toggle two SOUND OUTPUT devices.kmmacros (20.0 KB)

(function (strDeviceA, strDeviceB) {
	'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 = -1 !== c.indexOf(strDeviceA) ? strDeviceB : strDeviceA;
	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;


})(kmVar('soundDeviceONE').value(), kmVar('soundDeviceTWO').value());

function kmVar(k) {
	var kme = Application("Keyboard Maestro Engine"),
		vs = kme.variables,
		vks = vs.where({
			name: k
		});

	return vks.length ? vks[0] :
		vs.push(kme.Variable({
			'name': k
		})) && vs[k];
}
4 Likes