Posted by: skyshab | February 4, 2009

Wake and Sleep a Media Server from your iPhone

In a previous post, “3 Ways to Control a Headless Server From Your Dock“, I detailed ways of waking/sleeping a server, and mounting the desired volume easily from one’s dock. I use my iPhone as a remote for my HTPC, so it was only a matter of time before I found a way to do it from the comfort of my couch. Plus, the app that I was using, “Wake on LAN”, was proving to be a bit buggy.

While Wake on LAN has some great features such as scanning your entire network, and storing your passwords in Keychain, I would gladly trade it all in for something that works consistently. The problem that I had with WOL was that my Keychain password for my server magically disappeared every time I restarted WOL. I tried to get around this by leaving WOL running all the time, and while this took care of the password issue, there were still quite a few instances where the app didn’t work for other mysterious reasons. I contacted the devs at ReadPixel, and they claimed that the password issue was on Apple’s end, not theirs. Be that as it may, it doesn’t make me want to use the app, especially when there’s a working alternative.

One of the main requirements that I had for a working sleep/wake solution, was to use Applescript in the implementation. I am using the app “Snatch” as a remote for my Plex based media center, and had already integrated some other AppleScripts into the process using another app called “Fastscripts Lite“. Looking around, I found a couple of scripts to do what I needed, and it took only a few moments to add their functionality to my existing remote.

The only downside to my method, is it requires hard-coding the server’s password into the sleep script. However, since this is only being sent within my local network, and behind both hardware and software firewalls, I’m not too concerned about the security issues that it may pose.

The first goal: wake a sleeping server, and mount the volume that contains my media.

I found an Applescript written by Mark Muir, that sends a ‘magic packet’ to a sleeping computer, signaling it to wake up. The basic concept behind this is that it sends this packet of data over your network 16 times in a row, and if your computer has ‘Wake on LAN’ capabilities, it will respond. To enable this functionality on a Mac, simply check “Remote Apple Events” under Preferences>Sharing on the computer you wish to wake.

Mark Muir’s original script, which you can view HERE, contained a dialog box allowing you to input the MAC address of the machine you wanted to wake. Since I was only wanting to wake a single machine, I modified the script to skip the dialog step, and just get down to business. I also added a line to mount the volume with my media, near the end of the script. Here’s the modified script that I used:

property MAC_address : "00:1d:0a:7d:69:42"
property WAN_IP_address : ""
on run
set command to "/usr/bin/php -r " & quoted form of ("$mac = " & quoted form of MAC_address & "; $ip = " & quoted form of WAN_IP_address & "; " & "
$mac_bytes = explode(\":\", $mac);
$mac_addr = \"\";
for ($i=0; $i<6; $i++)
$mac_addr .= chr(hexdec($mac_bytes[$i]));
$packet = \"\";
for ($i=0; $i<6; $i++) /*6x 0xFF*/
$packet .= chr(255);
for ($i=0; $i<16; $i++) /*16x MAC address*/
$packet .= $mac_addr;
$port = 9;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);
do shell script command
mount volume "afp://"
end run

Of course, I put in a nonsense ip address and MAC address in this example, so you would need to change it out with your specific info.

The second goal: Put the server to sleep when I’m done.

This script is much simpler. Here is the basic format. You obviously need to replace name, password, and the ip address with the proper info.

do shell script (“hdiutil unmount /Volumes/MediaDisk“)

tell application "finder" of machine "eppc://name:password@" to sleep

I should mention that the sleep script will not compile correctly if the machine in question is not on and running while you are creating the Applescript. In fact, it will not even open up in script editor if the specified volume is not mounted. It took me several frustrating hours to figure out why it would not work properly. Grrrr…

To get up and running, copy each script into Script Editor, insert your ip address, user name,  password, and volume name where required. I have colored the parts of the scripts that you need to change with your info in green. Then compile the scripts and save them in Username>Library>Scripts folder. (you may have to create the folder if it does not exist). Link up the scripts to the desired keyboard keys in Fastscripts Lite, and create a couple of buttons in Snatch. For more detailed directions on how to accomplish this, see the example in this wiki.

Here’s a screenshot of the Snatch remote screen on my iPhone whose sole purpose is only to wake and sleep the server:


This works wonderfully with the custom remote that I have created for Plex:


Well, there you go. Wake or Sleep at the press of a button. How much easier can it get?



  1. How did you get the up, down, fast forward and rewind arrows into Snatch? The left right arrows are evident but not the others. Also your button sizes seems to be different than the ones I’m able to create. I think I’m using Snatch v2.3

    • Hey there.

      Not sure what you mean. The arrows are just mapped to…well, the arrows. Up=Up, Down=Down, etc.

      See the how-to wiki I wrote for more explicit info on setting the remote up.

      As far as the size, if you double-tap the button while in edit mode, you can drag it to be whatever size you want. This is a newer feature that I really like.

  2. I think the commenter was referring to the carats you used for the up and down. I was wondering that as well. Also, is there a way to change the font color of the text.

    By the way, great idea on this. Am just getting to know plex and finding this tool will be a great and useful addition.

  3. Aargh-a-Knot,

    I have read your how to do on the iphone control… having a small problem with it…. ctrl f15 keeps opening the brightness on the display monitor not to sure what I’m doing now.


    • Control f15 is just what I mapped to launch the script in Fastscripts Lite. You can map it to whatever you’d like. Try picking a key combination that is not already in use by the system.

  4. May I ask what you mapped your key to?

    • Umm…I mapped the script in the post to be launched by control F15, which is connected to a button in Snatch that wakes the server.

      Not sure if that answers your question?

  5. Aargh-a-knot,

    Man I’m so new to this… This is what I’v done so far. I followed your Doc to the (T)… I made a button on the iphone and name it Plex.
    Now on the mac mini keyboard if I hit control-f15 Plex’s will open. No problem on that end….. Now on the iphone I can’t seem to get it to work on the iphone, I mapped command f15 to the button on the iphone but how does that know to open plex

  6. Does this whole script thing work if my Mac Mini is only connected via wireless? ie. does the server have to be connected via ethernet to work, or can this wake a computer via wi-fi?


    • Unfortunately, WOL will only work with ethernet, as wifi shuts off when sleeping.

  7. I really like and use your Plex remote – the newest one with the yellow, round, reflecting buttons.
    [maybe you should update the screen shots on this post …;)]

    I was just wondering what you use to wake your machine running Plex … or is it always on?

  8. @Zwalex

    Yeah, I’m pretty lazy about updating this blog. Plex runs on my main computer, so there’s no need to wake it remotely. I have to manually switch the displays from my monitor to my projector anyway.

    I actually switched to having all my media drives in my main computer, so i don’t even use this function anymore.

  9. love the idea but have I missed something… how can you wake a computer by running an applescript on that computer? if the computer is asleep, you can’t run the script?

    • I guess you did miss something. 🙂 This is for waking and sleeping a media server computer, separate from the computer that the script is on.

      Anything in particular you are trying to do?

      • want to wake and sleep the media center, but without relying on any other computer, just the iPhone. I know apps exist that send the ‘magic packet’ for wakeup, and the applescript will work for sleep. but i want to do it all from the one app (Snatch) if I can…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: