Download
SleepWatcher 2.1 and unzip it to wherever.
In the unzipped folder is a README. Scroll down to the
Installation for new SleepWatcher users section for instructions on installation. As it says, there are two plist files in the config folder. You want to copy the *-user.plist version to
~/Library/LaunchAgent
Now, in your home folder you need to create three scripts -
.sleep,
.wakup and
wake
.sleep Script
Code:
#!/bin/sh
/usr/bin/osascript -e "tell application \"Finder\"" -e "eject \"Media\"" -e "end tell"
NB: osascript is the AppleScript interpreter, and the -e paramater is a separate line. So above we are building a 3 line applescript on the fly.
.wakeup Script
Code:
#!/bin/bash
/usr/bin/osascript ~/Scripts/wake.app
wake Script
Code:
on run
repeat 5 times
try
do shell script "curl http://windward"
mount volume "smb://windward/Media"
exit
on error
-- continue
end try
delay 2
end repeat
end run
With .wakeup I split it into two parts. The .wakeup script and a wake.app script. I could have built the wake.app script on the fly again, but since it was a larger script I opted not to.
Now basically this is how it works:
When SleepWatcher detects your MBP is going to sleep, it runs the .sleep script. That uses AppleScript to tell Finder to eject the Media drive. You can copy/paste the same line [altering the name of the drive to eject] for as many times as you need.
When your MBP wakes up again, SleepWatcher runs the .wakeup script. The .wakeup script calls the AppleScript wake.app. wake.app first of all checks if my MBP is connected to the network by contacting my server. You can substitute this address with your own intranet site or just do something like
www.google.com - I do this because sometimes the mount would fail as the script would run before the WiFi reconnected. It will keep checking for a connection before finally mounting a drive using Finder. The wake.app script as is will try to connect 5 times and then give up. You can change this by changing the line "repeat 5 times" to any number that suits you.
I could have used a straight forward mount/umount, but going via the Finder means I don't need to use any usernames or passwords in my scripts - it automatically uses the OS X keychain! Handy eh? And a little more secure
You will need to create wake.app yourself from the wake script. This is really easy - open wake with the AppleScript Editor [Applications > Utilities] and save it as an application. I saved it to ~/Scripts. Where you save it to is your choice - just remember to change the path in .wake
So far I have no issues with this which I am happy about. I also reuse the wake.app as one of my Login Items. That way I can manage how and what gets mounted from the app, and not mess about editing the Login Items list.
If you have problems just let me know
EDIT: I should point out the wake.app script is an infinite loop - this works fine for me since I will rarely, if ever, be taking my MBP out anywhere any time soon. But when I get chance I will change it so it will give up after a certain number of tries - this way it's not chewing up resources if you happen to be away from home.
EDIT EDIT: OK I have already tweaked it - was much easier than I thought. I have changed the wake script accordingly
