11/06/2007

Launching Aquamacs

After installing Leopard on most of my machines, I lost one key script that for some reason I wasn't keeping in my home directory. It was the script I use to launch Aquamacs from the command line. With this, I can open a file in an already running Aquamacs session (most of the time) or start up a new one entirely. Either way, it can all be done from the command line and it's super easy to type.

Here's the perl script that I use. Just save it as "e" or whatever, make it executable, and put it in your path.

#!/usr/bin/perl

$pid = 0;
open I, "ps -axww -U $ENV{'USER'} |";
while (<I>) {
  if (/Aquamacs Emacs/ && !/grep/) {
    if (/^\s*([0-9]+)\s/) {
      $pid = $1;
    }
  }
}

close I;
$args = "";
for my $f (@ARGV) {
  if (! -e $f) {
    system("touch \"$f\"");
  }
  $args .= "\"$f\" ";
}

# there is still an issue:
# if the sudo emacs is still open, it will
# call 'open' and open the files in the wrong
# emacs process.

if ($pid) {
  system("open -a /Applications/Aquamacs\\ Emacs.app $args");
} else {
  system("/Applications/Aquamacs\\ Emacs.app/Contents/MacOS/Aquamacs\\ Emacs $args &");
}

exit;

This is actually stolen from someone else, somewhere on the internet, but I don't know where it came from. If it was you, let me know and I'll give you all the credit and glory.