Amazon Dash button to call an Applescript

The other day Amazon put several of their Amazon Dash buttons on sale for $1.99. I picked up a few.

The first project I tackled was to get a Dash button to let everyone in the house know that the dog has been fed. This was pretty easy. 

  • Someone feeds the dog and presses the Dash button on the dog food bin
  • a computer on my network (a MacBook Air in my stereo cabinet) running Dasher listens for the Dash button to be pressed
  • that computer uses some java code to call a pre-defined IFTTT webhooks/Maker URL
  • that triggers a text message to everyone in the family that the dog has been fed

There are step by step instructions for doing much of this in this article. It is surprisingly easy and relies upon the Dasher project code

As I started looking at the Dasher code I realized that it also had a hook for ExecFile and that could be used to call a script/program etc instead of just calling a URL. Super!

Using an Amazon Dash Button to Play a Spotify Playlist

Most of the dash button hacks that people have documented rely on IFTTT. While IFTTT is no doubt super useful, I needed a bit more granularity in what I wanted my buttons to do and wanted to be able to call something more robust like a python or AppleScript file when the button was pressed. As a starter project, I wrote an AppleScript that checks the current time of day and then launches a playlist (and sets the volume) for that time of day. Unfortunately I could not get Dasher to work with the AppleScript.

I tried:

  • Saving the script as a compiled application and calling it in the command section, this just errored out and didn’t give me any clues
  • Then I wrapped the AppleScript in a shell script calling it with osascript, that errored out with a (-600) “application isn’t running” error which told me that at least the script was getting launched but wouldn’t run right
  • Then I realized that the process calling the shell script was running as root and as such the AppleScript was being called from root.
  • At first I tried all sorts of setuid BS on the shell script but that didn’t work
  • Then in an aha! moment I changed the shell script from ‘osascript /Users/jimwillis/bin/scripts/spotify_launch.scpt” to:
    • sudo -u jimwillis osascript /Users/jimwillis/bin/scripts/spotify_launch.scpt
  • Voila! It worked like a champ, I just needed to make sure that the AppleScript was being called/run as me, not root!

Calling an AppleScript from Dasher

So the key here is:

Setup Dasher to call a shell script in the dasher config.json file, like this:

{
"name": “Spotify-button",
"address": "78:E1:03:C5:D8:AF",
"cmd": "/Users/jimwillis/bin/scripts/spotify.sh",
"debug": false
}

Then, make sure that the AppleScript is running as you, not root by calling osascript with sudo -u {yourUserName}, so the shell script I’m calling is just a one-liner that looks like:

sudo -u jimwillis osascript /Users/jimwillis/bin/scripts/spotify_launch.scpt

Have fun!!  Huge thanks to John Maddox for writing Dasher and Jeff MacDonald for the great HowTo