In part 1, I showed how to set environment variables using a shell script to enable malloc debugging features in a Dashboard widget. For some dumb reason, this wasn’t good enough for me. No… I had to take this nice simple shell script, and re-write it in Ruby. The benefit? It’s easier to conditionally set environment variables, e.g. set MallocStackLogging only for certain widgets. This is way overkill, since it’s pretty easy to just edit the shell script with sudo when needed. It’s not like widgets are launched so often where this really matters. But what’s done is done, and I may as well share my big waste of time with the world:

#!/usr/bin/ruby -w

require 'etc'

def is_admin?
  username = Etc.getlogin
  return (Etc.getgrnam("admin").mem.include? username)
end

$DashboardClient = $0 + ".orig"
$ARGS = ARGV

conf_file = File.join(ENV['HOME'], ".MacOSX", "DashboardClientConfig.rb")
if is_admin? && (File.exist? conf_file)
    load conf_file
end

exec $DashboardClient, *$ARGS

The major difference is that all customization goes into ~/.MacOSX/DadshboardClientConfig.rb. The benefit is I don’t have to run sudo to change environment variables. Also, each user has their own customization. As a precaution, only users in the admin group may customize Dashboard. This way, you’re average user can’t accidentally shoot themselves in the foot. Overkill #1: A user isn’t going to “accidentally” create ~/.MacOSX/DadshboardClientConfig.rb without knowing what they are doing. Overkill #2: I’m the only user on my development machine. My excuse: I just wanted to learn how to use Ruby’s Etc class. But like I said, what’s done is done. So how is this used? Here’s a sample DashboardClientConfig.rb:

case $ARGS[0]
when /Hello\ World\.wdgt/
  ENV['MallocStackLogging'] = 'YES'
when /Goodbye\ World.wdgt/
  ENV['DYLD_INSERT_LIBRARIES'] = '/usr/lib/libMallocDebug.A.dylib'
end

Since the path to the actual widget is the first command line argument, you can do per-widget customization. So there you have it. An overly complicated method of setting environment variables.