Remotely Controlling the Volume on Your Mac
One of my favorite parts of Spotify Premium is that it allows you to play your music through other devices. During the harsh New England winters, when I like to sit in my bed and do work on my laptop, thanks to Spotify Premium I can still run music through my desktop computer and my speakers.
Unfortunately, this type of control is limited to the Spotify app. If you’re watching YouTube or Netflix, you’re forced between listening through your laptop’s weak speakers or even worse, actually getting out of bed to control the volume. It’s annoying.
So, I decided to fix this.
I built a set of tools that allows you to adjust the volume of your computer—remotely.
Part One: The Simple Solution
A quick Google search on “how to mute the volume on my mac with a script” led me to this helpful site, http://leafraker.com/2007/11/13/mute-system-volume-with-apple-script-and-quicksilver/.
After figuring out what Apple Script was, I quickly threw together a script that allowed me to mute and un-mute my own laptop. A few more adjustments let me increase and decrease the volume incrementally. Great.
Now for the harder part: applying the solution to my remote desktop computer.
I didn’t just want to copy the script over and SSH into my desktop every time I wanted to adjust the volume. That felt lame—like cheating. Plus, it was tedious. Who would want to have to SSH into their computer every time they wanted to change the audio levels?
A few more Google searches and I had a better solution: Remote Apple Events. Under System Preferences > Sharing > Remote Apple Events there was a setting that would allow you to trigger events on your computer remotely. Bingo.
Within a couple of minutes tweaking my code, I had a working product.
The commands were simple and handled through a script run on the command line.
Unfortunately, there was a problem. When I ran the script, it worked, but I was prompted with the below message:
Uh oh. We were right back where we started. This was the same as just SSHing in, albeit with a nicer graphical interface.
I didn’t want to have to enter my password (twice!) every time that I wanted to change the volume. I wanted to run one command and then forget about it.
I wasn’t ready to just give up yet. There had to be a better solution.
Part Two: The Convoluted Solution
I should preface this solution with a disclaimer. It’s hacky. But, it works. And it’s surprisingly versatile.
After prowling around Google some more for a better solution than Apple Remote Events, I found an interesting OS X feature that is rarely mentioned: Folder Actions.
Folder Actions enable the user to monitor select folders, triggering an automatic action when a new file is placed in one of those folders.
It’s a nifty idea. If you want to append the date and time to the name of the file every time you say a file in a certain folder, you can do it. Or, if you want your computer to automatically move every photo you download into your Downloads folder to your Photos folder, that can be done too.
In this case, I wanted my folder action to trigger a volume change.
But, how did I get a folder change to monitor changes sent from my laptop to my desktop computer?
The solution was Dropbox.
After creating a new Folder Action using the Mac’s automator app, I set it to monitor a Dropbox folder—a folder who’s contents I could alter remotely, from any device. Every time a new file popped up in that specific Dropbox folder, my desktop would know. If it was labeled something like “mute” or “unmute,” my desktop computer’s volume would change.
Working in automator itself was a pain. But, after a while, I got it to work. The exact program is shown below:
The program runs an Apple Script to extract the name of the new file saved in the folder. It then sends this filename to a shell script which calls an external Apple Script. The shell script also deletes the file that caused the trigger, keeping the Dropbox folder nice and clean.
(Even the Folder Action implementation above is a convoluted workaround. I could have just entered my original Apple Script into the “Run AppleScript” portion of the automator above, but for some reason, automator won’t adjust the size of the text entry box. In other words, I could only see 10 lines of code as I was editing it. I wasn’t a fan, so I just put my code elsewhere. If I get around to it, I’ll fix this.)
The Apple Script that was called used the name of the file to trigger the appropriate action. To simplify things, the files were interpreted in a slightly simpler (and less verbose) manner than in Version 1: a file named mute muted the volume, unmute unmuted the volume, and a number between -100 and 100 changed the volume in percentage increments. (Negative numbers decreased the volume, while positive numbers increased the volume.)
The Apple Script is shown below:
I also created a simple shell script to create the files for me.
(I guess I could have just typed echo “” > “[command]” on the command line as well, but I figured this was I could class it up a little.)
Now, I had a working solution. I could run a shell script from my laptop and change the volume of my desktop. If I needed silence for a short period of time while I was working, I no longer needed to hop out of bed to change the volume.
There was one catch though—because of the folder action, there is a slight delay (about 10 seconds) between the time when I create a file in the Dropbox and the time that the volume changes. It is possible to change the frequency of polling by the folder action command, but honestly I’m OK with a 10 second delay. I don’t think I need the incessant polling wasting some CPU time every second.
For an afternoon’s work, this was a nifty project. The best part about it is its versatility. I can add a file to my Dropbox folder anywhere—from my phone, iPad, or computer.
In the future, this infrastructure could be expanded to also manage playback on iTunes and YouTube. All playback could have the convenience of Spotify Premium.
I’ll try those next. Stay tuned.