or
Matt Webb matt@interconnected.org
Ingredients: kicker-smtp.xml and changeSMTPServers.pl.txt. Find out what they do further down.
We want to change the current SMTP server to a more appropriate one every time the network location is changed on Mac OS X.
Network System Configuration on Mac OS X has a high level Carbon framework called System Configuration which you can learn about at Apple's System Configuration Overview technote. It has a low level interface for shell-scripts governed by configd.
All of this information is pinched from this page on culater.net, but in a nutshell:
Notification
Add this xml fragment to the bottom of /System/Library/SystemConfiguration/Kicker.bundle/Resources/Kicker.xml. Put it just inside the last </array>. Do this as root.
In that xml fragmant:
Now reboot. I'd love to know how to restart configd and all its associated processes from the command line, but I can't figure out how.
Changing the SMTP server for mail
This is done with Applescript. Using the defaults system might work, but I'm not sure how to make Mail.app read the new values. So Applescript it is.
The script that's mentioned in the xml fragment above is here: changeSMTPServers.pl.txt. Put it wherever you said in Kickers.xml, and chmod it to execute (and knock the .txt off the end, obviously).
Configure the script
At the top of the script, there a hash called $smtp_for_location which maps existing network location names to existing SMTP servers. Change it to whatever you'd like.
This bit is important: The SMTP servers must be known by Mail.app already. Go into Mail, choose Preferences, edit an account and look at the Outgoing Mail Server list. Choose Add Server. The SMTP server mentioned in the script is just the identifier for all this information -- and it's the hostname used as the identifier.
The next bit of configuration is the $accounts_to_change list. Any Mail.app account name listed in here will be changed whenever the location changes.
What the script does
Gets the current location. Because configd doesn't pass through what's changed, only that something has changed, the script calls out to scutil and looks up the UserDefinedName of the current location. This is the location name you set in the Network control panel.
Puts together an Applescript. The Applescript needs to know the new SMTP server and which accounts to change. Those are put in as properties. The Applescript then cycles through the SMTP servers that Mail.app knows about, making sure this new one exists. If it does, it grabs the server object. It then cycles through the accounts, and changes the relevant ones if necessary. It also spits back some useful information about what was changed, but who knows how to pick that up.
Runs the Applscript. The Applescript is run via a backticked osascript. Lovely.
Mail.app needs to be running.
If you're multi-user on your machine, this script ain't going to work. It runs as a single user, and has no configuration to change to different SMTP servers for different users. Nasty.
Exceptions. Know how to get information back from osascript through configd and back to the user? No? Nor do I. That's a bug.
There are no guarantees. Don't muck around with your system if you don't know what you're doing, I take no responsibility.
Flames and letters of disgust to Matt Webb at matt@interconnected.org.
26 August 2002