2011-01-03

A Mac OS X app to wake a Mac and then open screen sharing

I have a Mac Mini hooked up to our TV and stereo as a HTPC which has a hard-wired Ethernet connection. To save a bit of energy I have it set to go to sleep when it isn't used. My wife wanted to be able to play internet radio stations over the stereo, but she didn't want to have to turn on the TV, get a keyboard, etc. just to start one playing. I showed her how to use one program to wake the Mac Mini, and then how to launch screen sharing through Finder, but it was a lot of clicking. So I started googling for a way to set up a one-click icon that would (1) wake the Mac Mini, and (2) then connect to it by screen sharing.

As to waking the Mac Mini, I found this page that gives an Automator workflow to send the wake on lan magic packet (which will only work if the target computer has a hard wired Ethernet connection) and also open screen sharing:

Wake sleeping Mac with AppleScript and Automator

The guts of this is a PHP script written by Mark Muir to send the magic packet that wakes the sleeping computer. The version of this PHP script at the link has comments explaining how it works. Since I have zero experience with Automator I decided to adapt this to be an AppleScript. Here is what I ended up with:

on run
set command to "/usr/bin/php -r " & quoted form of ("$mac = \"a4:6a:19:d8:d2:24\";
$ip = \"10.10.10.255\";
$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);
socket_close($sock);
")
do shell script command
delay 3
tell application "Screen Sharing"
open location "vnc://MyComputersBonjourName.local"
end tell
end run


To use this yourself do the following:

  • Make sure screen sharing is configured on the target computer and verify it works when you manually connect from the source computer.
  • Go to the target computer and open Network Utility and look up the Hardware Address of the target computers Ethernet network interface. Change the $mac value in the Applescript to be this value.
  • Look up the Bonjour name of the target computer (System Preferences -> Sharing : Computer Name) and substitute it for MyComputersBonjourName in the Applescript.
  • Change the $ip value in the Applescript to be the IP address of your router with 255 substituted for the last digits. For many people this would be 192.168.1.255. Also, 255.255.255.255 should work.
  • Open AppleScript Editor on the source computer (the one you want to connect from) and paste the script in it and then click Compile and then click Run to test it. It should wake the target computer and open screen sharing for it.
  • Save the AppleScript as an app by selecting File -> Save As and then picking application as the file type in the dialog box.
  • Double click on the app to verify it works.
  • Drag the app to the Dock or wherever you want it to live.