Play an audio file with a keyboard shortcut (Mac)

Recently I was in a position where I needed access to the Inception "WOOOMMMMPPPP" sound effect fairly often. There were sites like this which allowed me to click a button and play the sound effect, but I wanted to take it one step further. See, the thing is it's not always convenient to find a tab in Chrome and click the button. It's much easier to just use a keyboard shortcut.

Here is the script I made. It's very simple:

set previousVolume to output volume of (get volume settings)  
set volume output volume 100  
do shell script "afplay " & "~/inception.mp3"  
set volume output volume previousVolume  

Originally all the script did was play the inception sound effect. This was great except for when I realized 90% of the time my computer is muted at work. This means that the script effectively did nothing unless I raised my volume first, removing any benefit of the keyboard shortcut.

That's when I thought "Why can't I automate the setting of the volume before and after the sound effect?". And that's what happened.

This script now will capture the current volume setting, raise the volume all the way to 100%, play the audio file, then reset the volume back to where it was. Simple.

This script is probably so simple that it doesn't deserve a blog post, but I'm happy with it. Also, it's available on github as a gist.