Archives / Search ›

appswitch 1.0.1 and OS X speed improvements

appswitch 1.0.1 is out; the primary improvement involves a crashing bug I found and fixed about a month ago.

Part of appswitch’s README justifies its use by the speed advantage over alternate OS X command-line methods for bringing an application to the front. My timings were based on OS X 10.2.4, as of February 2003. I redid the same timings in OS X 10.3.3, adding my launch program, with the following results. Times are in seconds, as measured with time(1):

                                   Mac OS X 10.2.4  10.3.3
appswitch -a Emacs                          0.355   0.054
appswitch -i com.gnu.Emacs                  0.381   0.063
launch -a Emacs                             n/a     0.085
open -a Emacs                               0.842   0.876
osascript -e 'tell app "Emacs" to activate' 2.361   0.949

First, everything except open is a lot faster under Panther, especially appswitch and launch (for which I made essentially no code changes). AppleScript compilation and/or execution speed also improved dramatically, which is good to see.

Second, I’m surprised the huge performance gap between launch and open still exists. launch links to CoreFoundation, ApplicationServices, Carbon and Security frameworks; open to Cocoa. The tasks they perform are essentially equivalent, and I imagine call through to the same underlying APIs. This makes my original, somewhat arbitrary, choice to implement launch without using Cocoa a lot more defensible than it once was.

Some examples of how I use appswitch, from my ~/.zshrc.

Augmenting emacsclient to bring Emacs to the front:

  if [[ ! -z $TERM_PROGRAM ]] {
    e() {
      ( emacsclient -n $@ >&/dev/null && \
	appswitch -a Emacs ) || \
      ( launch -ba Emacs && \
  	until { emacsclient -n $@ >&/dev/null } do sleep 1; done;
        appswitch -a Emacs )
      export EDITOR=emacsclient
    }
    BROWSER='launch'
  } elif [[ ! -z $DISPLAY ]] {
    e() {
      ( gnuclient -q $@ >&/dev/null ) || \
      ( xemacs -f gnuserv-start $@ & )
    }
  } else {
    alias emacs="/usr/bin/emacs -nw"
    alias e=emacs
  }

Testing for a local X server before trying to enable SSH X forwarding:

ssh() {
  check_agent
  # appswitch takes about 0.2 seconds to run, so don't check
  # unless we're reasonably sure we have an X server running
  if [[ -z $DISPLAY && -S /tmp/.X11-unix/X0 ]] && appswitch -Pi com.apple.x11 >& /dev/null; then
    DISPLAY=':0.0' =ssh $@
  else
    =ssh $@
  fi
  chpwd
}

No comments yet. Be the first.

Leave a reply