Switch Audio Input/Output Device using Alfred
My top two must-have Mac apps are Keyboard Maestro and Alfred. If you know me, you’ve likely heard me say this, ad nauseum. I use these two apps countless times each day. Often I use them to run command line scripts via hotkey. This is one such implementation.
Recently I picked up a Jabra Evolve 65 headset. I keep it connected to my Mac via USB. Also, I have some speakers hooked up via the external speaker jack. You can only send output to one of these devices at a time (speakers OR headset). I desired a way to quick-switch between the two. I chose to use Alfred for this.
The steps below show how to create some quick switchers like this:
Requirements and Initial Setup
- Install https://github.com/deweller/switchaudio-osx (e.g., brew install switchaudio-osx )
- Run which switchaudiosource to see the full path to the binary
- Determine the names of your devices via switchaudiosource -a
- Find a good icon for the alfred workflow and the two triggers (a speaker and a headset should be fine; I use iconfinder to find free icons)
Alfred Workflow
Step 1: Create a new workflow
Name: Audio Switcher
Description: Switches audio input/output
Bundle ID: com.agileadam.alfredaudioswitcher
Created By: Adam Courtemanche
Website: https://www.agileadam.com
Step 2: Configure workflow and variables via the [x] icon at the top right of the workflow
Add About this Workflow text:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
INSTALLATION: Step 1: Install the “switchaudio-osx” application from: https://github.com/deweller/switchaudio-osx Step 2: Determine the device names you want to target: switchaudiosource -a Step 3: Update the /bin/bash actions to use correct paths and device names CREDITS: Icons are from Dinosoftlab on iconfinder.com |
Step 3: Create the headset keyword trigger and action
Setup the Keyword
Right-click in the main area and choose Inputs » Keyword
Keyword = headset
Title = Switch to headset audio
Subtext = Send and receive audio from headset
Icon = headset icon from iconfinder.com (copy and paste into the icon box)
Setup the Action
Right-click in the main area and choose Actions » Run Script
Language = /bin/bash with input as {query} running instances Sequentially
Script =
1 2 |
/usr/local/bin/switchaudiosource -s "Jabra EVOLVE 65" /usr/local/bin/switchaudiosource -s "Jabra EVOLVE 65" -t input |
Link the Keyword and the Action
Grab the handle on the right side of the headset Keyword object (when you hover you’ll see it).
Drag this over to the left side of the Run Script action object.
Step 4: Create the speakers keyword trigger and action
Setup the Keyword
Right-click in the main area and choose Inputs » Keyword
Keyword = speakers
Title = Switch to speakers
Subtext = Send audio to speakers
Icon = speaker icon from iconfinder.com (copy and paste into the icon box)
Setup the Action
Right-click in the main area and choose Actions » Run Script
Language = /bin/bash with input as {query} running instances Sequentially
Script =
1 |
/usr/local/bin/switchaudiosource -s "External Headphones" |
Link the Keyword and the Action
Grab the handle on the right side of the speakers Keyword object (when you hover you’ll see it).
Drag this over to the left side of the Run Script action object.
Step 5: Try it out!
But Wait… There’s More
As I was writing this blog post I realized perhaps it’d be better to offer a third method, which would toggle the audio device.
I’ll let you decide what type of trigger you want to use for this one; I’m leaning towards a hotkey.
The script would be pretty simple:
1 2 3 4 5 6 7 8 |
current_device=`/usr/local/bin/switchaudiosource -c` if [[ $current_device = "External Headphones" ]] then /usr/local/bin/switchaudiosource -s "Jabra EVOLVE 65" /usr/local/bin/switchaudiosource -s "Jabra EVOLVE 65" -t input else /usr/local/bin/switchaudiosource -s "External Headphones" fi |
3 Comments
Stuart moore
Thanks, this was just what I was looking for.
One tweak I’d suggest adding, is when setting up the Keyword, set ‘No Argument’ in the dropdown at top right. Otherwise Alfred will wait for a space and character before it’ll do anything.
Cheers
Stuart
Daniel
Hi,
I’m a huge fan of automation and an excessive AlfredApp user who also looked few times at Keyboard Maestro.
Why are you using Keyboard Maestro AND Alfred?
What unique features does Keyboard Maestro which aren’t included in Alfred?I’m really curious to see your use cases, to see what I have missed while my comparison.
Mark Beattie
Thanks, this was also exactly what I was looking for.
I whipped up a little workflow, and published it at https://github.com/beet/alfred_audio_switcher
cheers