Alfred 2 Workflow for Audio Device Selection

Alfred V2 has a great new feature called workflows. I’ve been playing with a few others wrote and had ported my old custom commands to workflows but was looking for something to really make use of their power.

nn

Changing OSX Audio device from menubarnChanging the selected audio output device was just the thing to implement as a workflow. OS X has a nice shortcut for doing this without heading to system preferences. Option clicking the speaker in the menu bar well let you change the input or output device, but I wanted to do it easily from the keyboard.

nn

The ‘Script Filter’ option under the input menu is the magic for this workflow. It allows a script to output a list of items for display and selection in the Alfred GUI. You get Alfred’s great predictive auto complete which makes selecting a device a breeze.

nn

Selecting output audio device in Alfred

nn

Output via Growl or Notification Center provides feedback of the completed action.

nn

Growl confirmation

nn

The workflow is pretty simple in Alfred’s workflow editor, it ends up looking like this:

nn

Alfred work flow

nn

Alfred makes sharing workflows insanely easy by throwing all the necessary files for each workflow in a dedicated directory. Right clicking a workflow in Alfred gives an export workflow option which creates a zip file of this directory with the ‘.alfredworkflow’ extension.

nn

Grab SwitchAudio.alfredworkflow

nn

The heavy lifting is done by switchaudio-osx by deweller on GitHub with a simple perl glue script to pass data back and forth between Alfred and switchaudio-osx.

nn

1n2n3n4n5n6n7n8n9n10n11n12n13n14n15n16n17n18n19n20n21n22n23n24n25n26n27n28n29n30n31n32n33n34n35n36n37n38n39n40n41n42n43n44n45n46n47n48n49n50n
#!/usr/bin/env perlnnuse strict;nuse warnings;nnuse FindBin;nnmy $action = shift || 'list';nmy $direction = shift || 'output';nnif ($action eq 'list') {n    my $devices = get_possibilities($direction);n    print qq{nn};n    print output_device($_) for @$devices;n    print "n";n}nnif ( $action eq 'set' ) {n    my $device = shift;n    print set_device($direction, $device) . "n";n}nnsub get_possibilities {n    my $direction = shift;n    my @devices;n    open( my $fh, '-|', $FindBin::Bin . '/SwitchAudioSource',n        '-at', $direction );n    while ( my $line = <$fh> ) {n        chomp $line;n        $line =~ s/ (Q$directionE)$//;n        push @devices, $line;n    }n    close $fh;n    return @devices;n}nnsub set_device {n    my ( $direction, $device ) = @_;n    open( my $fh, '-|', $FindBin::Bin . '/SwitchAudioSource',n        '-t', $direction, '-s', $device );n    my $output = join "n", <$fh>;n    close $fh;n    return $output;n}nnsub output_device {n    my $device = shift;n    ( my $device_no_space = $device ) =~ tr/ /_/;n    return qq{$deviceicon.pngn};n}n

nn