Audio Notifications in Alfred 2.0 Workflows

Since the beta release of Alfred 2.0 I’ve been working on several workflows that I’ll be sharing here (as well as in the Alfred 2.0 Beta Workflow forum). Some of these are practical workflows (such as Evernote Search+ which is still under development for version 1.1) and some are example workflows that I think might be of help to other Alfred workflow creators. Today I’m going to share a workflow that uses an audible notification at the end.

Currently (21/01/2013), Alfred 2.0 has four output options:

  1. Post Notification - Displays a text message (and title) in a notification centre or Growl notification. The notification can be optionally made persistent.
  2. Copy to Clipboard - Copies text to the operating system clipboard.
  3. Large Text - Displays a text message in a large font (think splash screen). It persists until you click somewhere on the screen on which it is displayed.
  4. Run Script - Runs a script (Bash, Perl, Php, Python, Ruby), optionally using some text as the input.

There’s no default action that creates an audible output - though I have submitted this as a feature request.

So why would you want an audible output? Well for those with visual impairments an screen readers that don’t work so well with notifications, the answer is probably rather obvious, but there are occasions when those without any visual impairment might also find it useful. I have a few custom workflows where scripts are called and they take some time to process. In the mean time I might change my focus to another screen or even leave my desk and having an audible alert to tell me that the operation has completed can be invaluable.

The solution is simply to use a script to trigger an audio file to be played. I’ve created an example workflow that shows how you might do this using a Bash script.

The script itself will play three types of audio notifications. If the keyword of “snd” is entered with no parameters it will play the default sound from within the script. This can be set to one of two audio files I created and deployed with this workflow (you could of course add your own) or one of the default Mac OS X sounds found in /System/Library/Sounds/. If a parameter is passed after the keyword snd, the script will assume it is a path to an audio file and play it. Please note I have not included any error handling in this script it will just fail silently.

The workflow also includes a visual notification. If it is playing a default sound the default notification will indicate this, otherwise the parameter (the file path of the audio file) will be displayed instead. The notification and audio script are in effect fed by the following script.

#Pass what ever argument was in Alfred on to the visual notification
#otherwise just pass "Playing default sound."

if [ -z "{query}" ]; then
    #No query parameter passed
    echo "Playing default sound"
else
    #A query parameter has been passed
    echo "{query}"
fi

The Bash script that runs the audio is shown below. Just uncomment the audio file you want to play and remember if you want to play your own audio file this will need to be added to your workflow folder.

INP={query}

if [ "$INP" = "Playing default sound" ];then

    #Play a default sound
    WORKFLOWPATH=`pwd`
    DEFAULTSOUNDPATH="/System/Library/Sounds"

    #Choose an audio file to play
    #   - Play a file: Uncomment it's line by removing the #
    #   - Do not play a file: Comment it's line by putting a # at the front

    #Play a file included with the workflow
    #PATHTOAUDIOFILE="$WORKFLOWPATH/dobedidoh.aiff"
    #PATHTOAUDIOFILE="$WORKFLOWPATH/tah_dahhhhh.aiff"

    #Play a file included with the Mac OS
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Basso.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Blow.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Bottle.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Frog.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Funk.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Glass.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Hero.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Morse.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Ping.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Pop.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Purr.aiff"
    PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Sosumi.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Submarine.aiff"
    #PATHTOAUDIOFILE="$DEFAULTSOUNDPATH/Tink.aiff"
else
    PATHTOAUDIOFILE={query}
fi

afplay "$PATHTOAUDIOFILE"

Hopefully you’ll find this workflow useful. If you do why not let me know?

Author: Stephen Millard
Tags: | alfred | scripting |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read