script

Run Mac Script on Wake from Sleep: Login to iChat

Having switched to the Mac for my work environment, I missed some of the built in scripting that Linux offers natively. Turns out, the Mac offers this too. There are a few minor hurdles to clear in order to start scripting on the Mac. The trick is the interplay between AppleScripting and shell scripts.

My problem was that iChat wasn't waking up after resume from sleep. You may say, "Use Adium!" But Adium crashes and crashes, and doesn't let me add new contacts. Tried the beta, etc.

So I wrote a script which logs into iChat and sets status to "available" that runs on resume from sleep.

First off, install MacPorts.

Next, sudo port install sleepwatcher

Sleepwatcher is invoked in daemon mode to run a command on wake, sleep, or other things. Check out the man page: man sleepwatcher It's pretty powerful.

In order to find out more about the commands that iChat would accept, I created a shortcut to Applescript editor (it's under Utilities) on the dock, then dragged iChat onto it, which revealed the Applescript Dictionary for that program. I tested this:

#!/bin/sh
osascript -e 'delay 10
tell application "iChat"
log in of service "youruseraccount"
log in of service "yourotheruseraccount"
set the status to available
end tell'

save as /path/to/wake.sh

Now, set up an Applescript to run that script. Paste in this code:

do shell script "/opt/local/sbin/sleepwatcher -w /path/to/wake.sh &> /dev/null &"

Compile it, then save it as an application. This program runs the AppleScript app you've created as an background process and runs the terminal command also. In this way you can add it as a login item.

Go to System Preferences -> Accounts -> Login Items and add it there.

Done. Now, ten seconds after login, iChat will log in and set status to available.

Capistrano and Drupal

Tom Lee's picture

I've been messing around with Capistrano over the last few days. The system is best known for deploying Rails applications, but at its heart it's really just a Ruby-based way of making it easier to manage repetitive server tasks over SSH.

There's one task in particular that I've used it to automate, and which you might find useful, too. Here at EchoDitto we're spread out over a number of offices — DC, Boston, New York, and often who-knows-where-else. This presents some development challenges when collaborating on sites built on a system that, like Drupal, puts alot of site configuration in the database, where version control systems can't be used to manage it.

Of course we do development locally when possible, but we also run a remote dev server to let us collaborate off a single database (and to allow clients to see our work). The obvious solution is to tunnel our local server's database traffic to the central server, allowing different developers' filesystems to connect to the same datastore. But this is slooow.

So other options: setting up MySQL replication? Well, that seems like overkill, and precludes offline development. The solution I've come up with is automating a simple database dump from the dev server. When I need to make config changes I do it on the dev site; when I'm working in the filesystem and need fresh data, I simply invoke a command to pull down the dev DB. For most sites this only takes a few seconds to run, and invoking it is as simple as running `cap get_db` from anywhere within the dev site directory structure.

I used to do this with a somewhat horrible bash script. But it made various assumptions about filesystem and had to be modified for each new local project. Capistrano has let me make this a bit more abstract and reusable. The following script still makes a few assumptions about our servers and configuration, but you might find it useful, too.