major cleanup, partial rewrite
osascript changed to support OS X 10.9